[rspec-users] Problem testing method

Carlos Rafael Belizón Ibáñez paliyoes at gmail.com
Wed Sep 24 11:02:36 EDT 2008


El mié, 24-09-2008 a las 09:22 -0500, David Chelimsky escribió:
> On Wed, Sep 24, 2008 at 9:21 AM, Matt Wynne <matt at mattwynne.net> wrote:
> > On 24 Sep 2008, at 14:38, David Chelimsky wrote:
> >
> >> On Wed, Sep 24, 2008 at 8:23 AM, Matt Wynne <matt at mattwynne.net> wrote:
> >>>
> >>> On 24 Sep 2008, at 13:35, Carlos Rafael Belizón Ibáñez wrote:
> >>>
> >>>> 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.
> >>
> >> Or you could use mock_model(..).as_null_object (assuming you have the
> >> current HEAD).
> >
> > This is the new record / playback stuff eh?
> 
> No relation. It's just a method that lets you say
> mock('foo').as_null_object instead of mock('foo', :null_object =>
> true).
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Sorry, before I wrote with errors the example (it's the problem if you
are remember without code at your face). This is the correct example
with the suggestions to fix the problem:

#foo.rb
class Foo < ActiveRecord::Base
  has_one :bar
  
  def foo
    self.bar.count -= 1
  end
end

#foo_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Foo do
  before(:each) do
    @bar = stub_model(Bar, :count => 1)
    @foo = Foo.new(:bar => @bar)
    @before_value = @foo.bar
  end
  
  it "should decrement bar.count in 1" do
    @foo.foo
    @foo.bar.count.should be_equal(@value_before - 1)
  end
end

#bar.rb
class Bar < ActiveRecord::Base
end

And now, I got this error:
1)
NoMethodError in 'Foo should decrement bar.count in 1'
undefined method `count=' for #<Bar id: 1001, created_at: nil,
updated_at: nil>
/home/carlos/NetBeansProjects/RailsApplication1/app/models/foo.rb:5:in
`foo'
/home/carlos/NetBeansProjects/RailsApplication1/spec/models/foo_spec.rb:11:

What it's wrong? Can I make this type of test with mock or stub?



More information about the rspec-users mailing list