I’m trying to test my views using rspec. I want to test my edit- and
new-view also for the case an error occurs (something like “title can’t
be blank” and so on).
Can someone point me please to an example where I can see how to mock my
model and stub all methods needed for the view?
I’m trying to test my views using rspec. I want to test my edit- and
new-view also for the case an error occurs (something like “title
can’t be blank” and so on).
Can someone point me please to an example where I can see how to
mock my model and stub all methods needed for the view?
If you happen to use Cucumber as well, I strongly suggest you check
the error messages there. First of all, it’s easier since you don’t
have to go mad stubbing and mucking about (sorry, had to make that
joke :P), and error messages more or less belong into integration
tests. At least, that’s my opinion.
I’m trying to test my views using rspec. I want to test my edit- and
new-view also for the case an error occurs (something like “title
can’t be blank” and so on).
Can someone point me please to an example where I can see how to
mock my model and stub all methods needed for the view?
I guess this isn’t exactly what you want to hear, but I would counsel
you against mocking your whole model object for a form with lots of
fields - those kind of ‘broad’ mocks can be very brittle to changes in
your domain model.
Can you try using stub_model instead? This creates an instance of your
actual model class, but with a crippled database connection. You can
then create an invalid model object and throw it to the view.
Something like:
assigns[:book] = stub_model(Book, :title => ‘’)
If your model is at all interesting, you might want to keep the
attributes that make a valid Book somewhere:
I’m trying to test my views using rspec. I want to test my edit- and
new-view also for the case an error occurs (something like “title
can’t be blank” and so on).
Can someone point me please to an example where I can see how to
mock my model and stub all methods needed for the view?
def stub_book(attributes = {})
stub_model Book, {
:title => “Test Book Title”,
:author => “Test Author”
}.merge(attributes)
end
Why hide them behind a helper? Why not just only specify them in
examples where they are used? ie:
it “should display the book title” do @book.stub!(:title).and_return “The Scarlet Letter”
render …
response.should include_text(“The Scarlet Letter”)
end
I’m trying to test my views using rspec. I want to test my edit- and
new-view also for the case an error occurs (something like “title can’t be
blank” and so on).
Can someone point me please to an example where I can see how to mock my
model and stub all methods needed for the view?
This gist is to show you how-to verify errors are displayed in a view,
not necessarily that you should go stubbing a bunch of superfluous
errors that may exist in the model to ensure they all show up. You
should only need to know that the errors are being displayed and one
dummy error message does that.
I’m trying to test my views using rspec. I want to test my edit- and
new-view also for the case an error occurs (something like “title can’t be
blank” and so on).
Can someone point me please to an example where I can see how to mock my
model and stub all methods needed for the view?
Here’s two ways to approach writing examples against ensuring errors
show up:
blank" and so on).
model.
If your model is at all interesting, you might want to keep the
it “should display the book title” do @book.stub!(:title).and_return “The Scarlet Letter”
render …
response.should include_text(“The Scarlet Letter”)
end
Why hide them in a helper? because I don’t want the noise of all the
attributes needed to make a valid book in each test.
@book = stub_book :title => “The Scarlet Letter”
This is basically the same pattern people use things like factory_girl
for, just using stub_model.
new-view also for the case an error occurs (something like "title can’t
Can you try using stub_model instead? This creates an instance of your
:title => “Test Book Title”,
}.merge(attributes)
Why hide them in a helper? because I don’t want the noise of all the
attributes needed to make a valid book in each test.
@book = stub_book :title => “The Scarlet Letter”
This is basically the same pattern people use things like factory_girl for,
just using stub_model.
Ah, I’m with you now. I thought you were eluding to doing the
following (but I realize now that wasn’t what you were getting at):
I’m trying to test my views using rspec. I want to test my edit- and
valid_book = {
:author => “Test Author”
end
Why hide them in a helper? because I don’t want the noise of all the
attributes needed to make a valid book in each test.
Also so that when you change validations, you only have to change one
part of test code instead of every test that uses that model.
Right, it wasn’t what I thought Matt was getting at. I thought he was
specifically referring to hiding things pertinent in a view spec
behind a helper. But that turned out to not be the case,
new-view also for the case an error occurs (something like "title can’t
Can you try using stub_model instead? This creates an instance of your
:title => “Test Book Title”,
}.merge(attributes)
Why hide them in a helper? because I don’t want the noise of all the
attributes needed to make a valid book in each test.
Also so that when you change validations, you only have to change one
part of test code instead of every test that uses that model.
Pat
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.