ruby - Rspec test to check input is within range 1-4 works once then fails -


my rspec test fails no matter input is. running test make sure input between 1-4 , seems work when input 3?

require 'order' describe order   'gets dish input'     order = order.new     allow(order.input).to receive(:gets).and_return(:name)   end    'checks input'     order = order.new     expect(order.input).to eq (1..4)   end  end 

calling order.rb

class order   def initialize     input   end    def input     puts 'please select number of dish'     gets.chomp   end    def valid_range? input     input.to_i.between?(1..4)   end end 

thank you

you write order class :

class order   attr_reader :dish_number    def initialize     @dish_number = input.to_i   end    def valid_dish?     dish_number.between?(1, 4)   end    private    def input     puts 'please select number of dish'     gets.chomp   end end 

and corresponding spec should :

require_relative '../order.rb'  describe order   'gets dish number input'     allow_any_instance_of(order).to receive(:input).and_return("1")     order = order.new     expect(order.dish_number).to eq(1)   end    'checks valid dish'     allow_any_instance_of(order).to receive(:input).and_return("1")     order = order.new     expect(order).to be_valid_dish   end  end 

result :

[arup@ruby]$ rspec spec/order_spec.rb ..  finished in 0.00946 seconds (files took 0.12904 seconds load) 2 examples, 0 failures [arup@ruby]$ 

as @myron said, here dry version of code :

require_relative '../order.rb'  describe order   def new_order(input)     order = order.new     allow(order).to receive(:input).and_return(input)     order   end    'gets dish number input'     expect(new_order("1").dish_number).to eq(1)   end    'checks valid dish'     expect(new_order("1")).to be_valid_dish   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 -