mocking in rspec

R

Ruby Pasaulyje

I have a problem with mocking. I have class DistanceMatrix and I would
like to indicate which method form_matrix is called in if/else
statement. I need to use mocha and rspec. Any 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
 
R

Richard Conroy

[Note: parts of this message were removed to make it a legal post.]

I have a problem with mocking. I have class DistanceMatrix and I would
like to indicate which method form_matrix is called in if/else
statement. I need to use mocha and rspec. Any ideas?
Is the Mocha cheat sheet much help?
http://cheat.errtheblog.com/s/mocha/


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
 
J

Jeremy Bopp

I have a problem with mocking. I have class DistanceMatrix and I would
like to indicate which method form_matrix is called in if/else
statement. I need to use mocha and rspec. Any 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

If you really need to tie your tests to such implementation details,
perhaps you could also mock get_data_from_yaml and get_data_from_db and
check to see which is called each time form_matrix is called.

-Jeremy
 
R

Richard Conroy

[Note: parts of this message were removed to make it a legal post.]

No. There are simple use of mocha in sheet, but in my case i need to use
mocha for method of object which wasn't received by parameters.
You are in a bit of a no mans land when it comes to mocking then. I would
consider breaking up that initializer so that the decision can be tested
seperately.

Alternatively you might be able to use 'any_instance':

DataMatrix.any_instance.stubs:)form_matrix)
DataMatrix.any_instance.expects:)get_data_from_yaml).with(String,
'whatever')
DataMatrix.new String, 'whatever'

You will need a lot of tests, to cover that decision process. I would be
tempted to break up that initializer or refactor it somehow.
 
R

Ruby Pasaulyje

Alternatively you might be able to use 'any_instance':
DataMatrix.any_instance.stubs:)form_matrix)
DataMatrix.any_instance.expects:)get_data_from_yaml).with(String,
'whatever')
DataMatrix.new String, 'whatever'

I tried:

describe DistanceMatrix, "when mocking ..." do
it "should do call form_matrix" do

DistanceMatrix.any_instance.stubs:)form_matrix)
DistanceMatrix.any_instance.should_receive:)get_data_from_yaml).with("file_name.yml")
DistanceMatrix.new("file_name.yml")

end
end

but got failure:

Failures:
1) DistanceMatrix when mocking ... should do call form_matrix
Failure/Error:
DistanceMatrix.any_instance.should_receive:)get_data_from_yaml).with("file_name.yml")
(#<Mocha::ClassMethods::AnyInstance:0xa153e48>).get_data_from_yaml("file_name.yml")
expected: 1 time
received: 0 times
# ./tsp_algorithm_spec.rb:275:in `block (2 levels) in <top
(required)>'

Finished in 0.32959 seconds

Also both methods (get_data_from_yaml and get_data_from_db) return Array
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top