On 24 Sep 2008, at 13:35, Carlos Rafael Belizón Ibáñez wrote:
Hi, I have one problem testing one method to learn rSpec. This is the
example
#foo.rb
class Foo < ActiveRecord::Base
has_one :bar
def foo
@bar.bar -= - 1
end
end
#bar.rb
class Bar < ActiveRecord::Base
end
#foo_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Foo do
before(:each) do
@bar = mock_model(Bar, :bar= => 1)
@foo = Foo.new(:bar => @bar)
@before_value = @foo.bar
end
it "should foo" do
@foo.foo.should be_equal(@value_before - 1)
end
end
I got this error:
1)
Spec::Mocks::MockExpectationError in 'Foo should foo'
Mock 'Bar_1001' received unexpected message :bar with (no args)
/home/carlos/NetBeansProjects/prueba/app/models/foo.rb:5:in `foo'
/home/carlos/NetBeansProjects/prueba/spec/models/foo_spec.rb:11:
How can I do this test using mocks?
try using stub_model instead of mock_model when you create @bar
If you create a mock object using mock_model(), or mock(), you have
to stub or mock absolutely all the interactions it will have with the
class under test. In this case, when you call Foo#foo, it calls the
#bar method on your mock Bar, which hasn't been set up to expect that
call.
So either use stub_model(Bar), which will create an instance of your
concrete Bar class (and will therefore more likely know what to do
when #bar is called), or remember to call @bar.stub!("bar-=") or
whatever.
Make sense?
Thanks ;).
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users
cheers,
Matt
----
http://blog.mattwynne.net
http://songkick.com
In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users