Testing gets in rspec (user input)












0















My class has this #run method that so far is just this, to test the testing:



def run
puts "Enter 'class' to create a new class."
input = $stdin.gets.chomp
binding.pry


And in the tests so far I've got



  allow($stdin).to receive(:gets).and_return 'class'
cli.run


Doing it this way I am able to see, in the pry session, that input has been set to 'class', as intended.



Is there a way to do with without adding $stdin to my call to gets in my method itself? i.e., input = gets.chomp



I've tried allow(cli.run).to receive(:gets).and_return 'class'
But then in the pry session, input is equal to the first line of the spec file!










share|improve this question



























    0















    My class has this #run method that so far is just this, to test the testing:



    def run
    puts "Enter 'class' to create a new class."
    input = $stdin.gets.chomp
    binding.pry


    And in the tests so far I've got



      allow($stdin).to receive(:gets).and_return 'class'
    cli.run


    Doing it this way I am able to see, in the pry session, that input has been set to 'class', as intended.



    Is there a way to do with without adding $stdin to my call to gets in my method itself? i.e., input = gets.chomp



    I've tried allow(cli.run).to receive(:gets).and_return 'class'
    But then in the pry session, input is equal to the first line of the spec file!










    share|improve this question

























      0












      0








      0








      My class has this #run method that so far is just this, to test the testing:



      def run
      puts "Enter 'class' to create a new class."
      input = $stdin.gets.chomp
      binding.pry


      And in the tests so far I've got



        allow($stdin).to receive(:gets).and_return 'class'
      cli.run


      Doing it this way I am able to see, in the pry session, that input has been set to 'class', as intended.



      Is there a way to do with without adding $stdin to my call to gets in my method itself? i.e., input = gets.chomp



      I've tried allow(cli.run).to receive(:gets).and_return 'class'
      But then in the pry session, input is equal to the first line of the spec file!










      share|improve this question














      My class has this #run method that so far is just this, to test the testing:



      def run
      puts "Enter 'class' to create a new class."
      input = $stdin.gets.chomp
      binding.pry


      And in the tests so far I've got



        allow($stdin).to receive(:gets).and_return 'class'
      cli.run


      Doing it this way I am able to see, in the pry session, that input has been set to 'class', as intended.



      Is there a way to do with without adding $stdin to my call to gets in my method itself? i.e., input = gets.chomp



      I've tried allow(cli.run).to receive(:gets).and_return 'class'
      But then in the pry session, input is equal to the first line of the spec file!







      ruby rspec mocking






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 19:56









      Jonathan TuzmanJonathan Tuzman

      532618




      532618
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can avoid this as such:



          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end

          describe 'gets' do
          it 'belongs to Kernel' do
          allow_any_instance_of(Kernel).to receive(:gets).and_return('class')
          expect(run).to eq('class')
          end
          end


          The method gets actually belongs to the Kernel module. (method(:gets).owner == Kernel). Since Kernel is included in Object and almost all ruby objects inherit from Object this will work.



          Now if run is an instance method scoped in a Class I would recommend scoping the stubbing a bit more such that:



          class Test
          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end
          end

          describe 'gets' do
          it 'can be stubbed lower than that' do
          allow_any_instance_of(Test).to receive(:gets).and_return('class')
          expect(Test.new.run).to eq('class')
          end
          # or even
          it 'or even lower than that' do
          cli = Test.new
          allow(cli).to receive(:gets).and_return('class')
          expect(cli.run).to eq('class')
          end
          end


          Example






          share|improve this answer


























          • Thanks! allow(cli).to receive(:gets).and_return('class') is exactly the kind of thing I was looking for, and it works.

            – Jonathan Tuzman
            Nov 28 '18 at 23:01












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527160%2ftesting-gets-in-rspec-user-input%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can avoid this as such:



          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end

          describe 'gets' do
          it 'belongs to Kernel' do
          allow_any_instance_of(Kernel).to receive(:gets).and_return('class')
          expect(run).to eq('class')
          end
          end


          The method gets actually belongs to the Kernel module. (method(:gets).owner == Kernel). Since Kernel is included in Object and almost all ruby objects inherit from Object this will work.



          Now if run is an instance method scoped in a Class I would recommend scoping the stubbing a bit more such that:



          class Test
          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end
          end

          describe 'gets' do
          it 'can be stubbed lower than that' do
          allow_any_instance_of(Test).to receive(:gets).and_return('class')
          expect(Test.new.run).to eq('class')
          end
          # or even
          it 'or even lower than that' do
          cli = Test.new
          allow(cli).to receive(:gets).and_return('class')
          expect(cli.run).to eq('class')
          end
          end


          Example






          share|improve this answer


























          • Thanks! allow(cli).to receive(:gets).and_return('class') is exactly the kind of thing I was looking for, and it works.

            – Jonathan Tuzman
            Nov 28 '18 at 23:01
















          1














          You can avoid this as such:



          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end

          describe 'gets' do
          it 'belongs to Kernel' do
          allow_any_instance_of(Kernel).to receive(:gets).and_return('class')
          expect(run).to eq('class')
          end
          end


          The method gets actually belongs to the Kernel module. (method(:gets).owner == Kernel). Since Kernel is included in Object and almost all ruby objects inherit from Object this will work.



          Now if run is an instance method scoped in a Class I would recommend scoping the stubbing a bit more such that:



          class Test
          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end
          end

          describe 'gets' do
          it 'can be stubbed lower than that' do
          allow_any_instance_of(Test).to receive(:gets).and_return('class')
          expect(Test.new.run).to eq('class')
          end
          # or even
          it 'or even lower than that' do
          cli = Test.new
          allow(cli).to receive(:gets).and_return('class')
          expect(cli.run).to eq('class')
          end
          end


          Example






          share|improve this answer


























          • Thanks! allow(cli).to receive(:gets).and_return('class') is exactly the kind of thing I was looking for, and it works.

            – Jonathan Tuzman
            Nov 28 '18 at 23:01














          1












          1








          1







          You can avoid this as such:



          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end

          describe 'gets' do
          it 'belongs to Kernel' do
          allow_any_instance_of(Kernel).to receive(:gets).and_return('class')
          expect(run).to eq('class')
          end
          end


          The method gets actually belongs to the Kernel module. (method(:gets).owner == Kernel). Since Kernel is included in Object and almost all ruby objects inherit from Object this will work.



          Now if run is an instance method scoped in a Class I would recommend scoping the stubbing a bit more such that:



          class Test
          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end
          end

          describe 'gets' do
          it 'can be stubbed lower than that' do
          allow_any_instance_of(Test).to receive(:gets).and_return('class')
          expect(Test.new.run).to eq('class')
          end
          # or even
          it 'or even lower than that' do
          cli = Test.new
          allow(cli).to receive(:gets).and_return('class')
          expect(cli.run).to eq('class')
          end
          end


          Example






          share|improve this answer















          You can avoid this as such:



          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end

          describe 'gets' do
          it 'belongs to Kernel' do
          allow_any_instance_of(Kernel).to receive(:gets).and_return('class')
          expect(run).to eq('class')
          end
          end


          The method gets actually belongs to the Kernel module. (method(:gets).owner == Kernel). Since Kernel is included in Object and almost all ruby objects inherit from Object this will work.



          Now if run is an instance method scoped in a Class I would recommend scoping the stubbing a bit more such that:



          class Test
          def run
          puts "Enter 'class' to create a new class."
          input = gets.chomp
          end
          end

          describe 'gets' do
          it 'can be stubbed lower than that' do
          allow_any_instance_of(Test).to receive(:gets).and_return('class')
          expect(Test.new.run).to eq('class')
          end
          # or even
          it 'or even lower than that' do
          cli = Test.new
          allow(cli).to receive(:gets).and_return('class')
          expect(cli.run).to eq('class')
          end
          end


          Example







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 21:46

























          answered Nov 28 '18 at 21:38









          engineersmnkyengineersmnky

          13.8k12241




          13.8k12241













          • Thanks! allow(cli).to receive(:gets).and_return('class') is exactly the kind of thing I was looking for, and it works.

            – Jonathan Tuzman
            Nov 28 '18 at 23:01



















          • Thanks! allow(cli).to receive(:gets).and_return('class') is exactly the kind of thing I was looking for, and it works.

            – Jonathan Tuzman
            Nov 28 '18 at 23:01

















          Thanks! allow(cli).to receive(:gets).and_return('class') is exactly the kind of thing I was looking for, and it works.

          – Jonathan Tuzman
          Nov 28 '18 at 23:01





          Thanks! allow(cli).to receive(:gets).and_return('class') is exactly the kind of thing I was looking for, and it works.

          – Jonathan Tuzman
          Nov 28 '18 at 23:01




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527160%2ftesting-gets-in-rspec-user-input%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)