ruby - RSpec test for a method that contains gets.chomp -


how design rspec test assigning gets.chomp method instance variable?

def choose     puts "please enter type want:"     @type = gets.chomp     puts "thank you, please enter how many of want:"     @quantity = gets.chomp end 

you can use stubs/mocks that. main question is: did place def choose? it's important since i'll stub it's calls on object.

let imagine have method in class item:

class item   def choose     puts "please enter type want:"     @type = gets.chomp     puts "thank you, please enter how many of want:"     @quantity = gets.chomp   end end 

then i'll able stub gets , chomp calls simulate user's input:

rspec.describe item   describe '#choose'     before       io_obj = double       expect(subject)         .to receive(:gets)         .and_return(io_obj)         .twice       expect(io_obj)         .to receive(:chomp)         .and_return(:type)       expect(io_obj)         .to receive(:chomp)         .and_return(:quantity)     end      'sets @type , @quantity according user\'s input'       subject.choose        expect(subject.instance_variable_get(:@type)).to eq :type       expect(subject.instance_variable_get(:@quantity)).to eq :quantity     end   end end 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -