[rspec-users] Problem testing method

Carlos Rafael Belizón Ibáñez paliyoes at gmail.com
Wed Sep 24 08:35:50 EDT 2008


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?

Thanks ;).



More information about the rspec-users mailing list