Ruby. Mocking in RSpec -
i have problem mocking. have class distancematrix , indicate method form_matrix called in if/else statement. need use mocha , rspec. ideas?
class distancematrix def initialize(*args) if args[0].class == string form_matrix(get_data_from_yaml(args[0], args[1])) elsif args[0].class == array || args[0] == nil form_matrix(get_data_from_db(args[0])) end end def form_matrix(...) ... end end
it tried:
describe distancematrix, "when mocking ..." "should call form_matrix" distancematrix.any_instance.expects(:form_matrix).with([1]).once distancematrix.any_instance.expects(:get_data_from_yaml).with("file_name.yml").once.returns([1]) distancematrix.new("file_name.yml") end end
but got error:
failures: 1) distancematrix when mocking ... should call form_matrix failure/error: distancematrix.new("file_name.yml") unexpected invocation: #<anyinstance:distancematrix>.get_data_from_yaml('file_name.yml', nil) unsatisfied expectations: - expected once, not yet invoked: #<anyinstance:distancematrix>.get_data_from_yaml('file_name.yml') - expected once, not yet invoked: #<anyinstance:distancematrix>.form_matrix([1]) satisfied expectations: - allowed number of times, invoked once: #<distancematrix:0x9e48b40>.get_optimal_route(any_parameters) - allowed number of times, invoked once: #<database::distances:0x9d59798>.load_distances(any_parameters) # ./distance_matrix.rb:18:in `initialize' # ./tsp_algorithm_spec.rb:253:in `new' # ./tsp_algorithm_spec.rb:253:in `block (2 levels) in <top (required)>' finished in 0.25979 seconds
i found in rspec should use not .expects() .should_receive(), tried:
describe distancematrix, "when mocking ..." "should call form_matrix" distancematrix.any_instance.should_receive(:form_matrix).with([1]) distancematrix.any_instance.should_receive(:get_data_from_yaml).with("file_name.yml").and_return([1]) distancematrix.new("file_name.yml") end end
but got new failure:
failures: 1) distancematrix when mocking ... should call form_matrix failure/error: distancematrix.any_instance.should_receive(:form_matrix).with([1]) (#<mocha::classmethods::anyinstance:0x96356b0>).form_matrix([1]) expected: 1 time received: 0 times # ./tsp_algorithm_spec.rb:251:in `block (2 levels) in <top (required)>' finished in 0.26741 seconds
i have experience using mocha , not rspec, looking @ mocha failure message, key parts these :-
unexpected invocation: #<anyinstance:distancematrix>.get_data_from_yaml('file_name.yml', nil) unsatisfied expectations: - expected once, not yet invoked: #<anyinstance:distancematrix>.get_data_from_yaml('file_name.yml')
if @ ends of these lines, notice get_data_from_yaml
not being called expected parameters. being called ('filename.yml', nil)
, not ('filename.yml')
expected.
this happening because when call distancematrix.new("file_name.yml")
in test one argument , inside distancematrix#initialize
distancematrix#get_data_from_yaml
being called (args[0], args[1])
, since args
single element array, args[1]
nil
.
maybe isn't how expected ruby work, following demonstrates behaviour :-
def foo(*args) puts "args[0]=#{args[0].inspect}; args[1]=#{args[1].inspect}" end foo("string") # => args[0]="string"; args[1]=nil
Comments
Post a Comment