I've started a fresh rails app with Employee belongs_to Company.
Here is the spec:
describe Employee do
example "stub should work with find(id)" do
company = mock_model Company
Company.stub!(:find).with(company.id).and_return company
employee = Employee.new company_id: company.id
employee.company.should == company
end
end
And here is the result:
Failures:
1) Employee stub should work with find(id)
Failure/Error: employee.company.should == company
<Company(id: integer, name: string, created_at: datetime, updated_at:
datetime) (class)> received :find with unexpected arguments
expected: (1001)
got: (1001, {:conditions=>nil})
Writing any of the following is not elegant:
Company.stub!(:find).with(company.id, conditions: nil).and_return company
or
Company.stub!(:find).with{|id, *args| id == company.id }.and_return
company # find could be called like Company.find(1, 2, 3) which should
return an array instead
or
Company.stub!(:find).with{|id, *args| id == company.id && (args.size
== 0 || (args.size == 1 && args[0].is_a?(Hash)) }.and_return company
So, it would be great to add some syntax to rspec-rails like:
Company.stub!(:find).with_id(company.id).and_return company
or
Company.stub_find_with!(company.id).and_return company
I'm not sure yet what changes would be better, but the current
implementation makes it very hard to read specs like this that are so
common when mocking models.
What do you think?
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users