Mocking in rspec

I have a problem with mocking. I have class DistanceMatrix and I would
like to indicate which method form_matrix was 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

On Wed, Dec 1, 2010 at 6:21 PM, Ruby P. [email protected]
wrote:

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/

Is the Mocha cheat sheet much help?
http://cheat.errtheblog.com/s/mocha/

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.

On 12/1/2010 12:21 PM, Ruby P. wrote:

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

On Wed, Dec 1, 2010 at 7:05 PM, Ruby P. [email protected]
wrote:

Is the Mocha cheat sheet much help?
http://cheat.errtheblog.com/s/mocha/

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.

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