On Thu, Feb 28, 2008 at 4:04 AM, nathan sharkey
[email protected] wrote:
validates_existence_of :company
@company = mock_model(Company)
@location - mock_model(Listing)
company.should_receive(:id).and_return(1)
listings.should_receive(:id).and_return(1)
@listing = Lisitng.new(:company_id => @company.id, :location_id =>
@location.id
You don’t really need mocks at all for describing the validation
behaviour. Just create a listing, get it into the state you want and
expect it to be valid or invalid as appropriate:
describe “a published Listing” do
def valid_attributes
{:title => “A Great Read”,
:company => Company.new,
:listing => Listing.new}
end
before(:each) do
@listing = Listing.new(valid_attributes)
@listing.stub!(:published?).and_return(true)
end
it “should be valid with valid attributes” do
@listing.should be_valid
end
it “should be invalid without a title” do
@listing.title = nil
@listing.should_not be_valid
end
… etc
end
You could also write a custom matcher that lets you do stuff like this:
@listing.should_not be_valid_with(valid_attributes).less(:title)
Come to think of it, maybe we should just add that to rspec_on_rails!
(Lighthouse - Beautifully Simple Issue Tracking)
Cheers,
David
Are there any multi model examples of applications out there with
Rspec tests that I could study or Books maybe?
I am working on an RSpec book that will have some material on Rails.
This issue will definitely be addressed, but as I said before -
there’s no one right answer, so don’t expect silver bullet solutions.
In the mean time, there are a few good books on TDD, which is part of
the basis for BDD. In fact, if you read them you’ll see things like
“this is not a book about testing” and “focus on behaviour.” Here are
a couple of recommendations:
Test-Driven Development by Example (Kent Beck)
Test-Driven Development, a Practical Guide (Dave A.)
The won’t teach you about Rails directly, but they do address
decoupling and finding a related balance.
I think this would
help me greatly understand what to test and what not to and how tests
are de coupled correctly. I’d love to see one made with stories
too,
There will be material in my book on Stories. Early in the book there
is a worked example, developing a small command line application using
stories together with object specs.
I’ve been writing stories but haven’t tried to implemented them
yet, figured it would make sense to understand model tests and then
work my way up… stories fascinate me because they get me thinking
about how it will look and feel to the user not just the behaviour.
Maybe I should be doing things top down from stories but I feel that
is a bit advanced for now
That’s the trick. Often things that produce a simple and clear result
have to do some complex things under the hood. This is true of all of
the components in Rails, and certainly true of RSpec stories as well.
cheers again for your input…
My pleasure.
Cheers,
David