I have a request spec that looks like this:
describe "Registering from the main page" do
before(:each) do
fill_in "username","my_username"
fill_in "password","my_password"
fill_in "password_confirmation","my_password"
click_button "Register"
end
it "should have created a new user" do
User.count.should == 1
end
it "should have changed the message on the homepage" do
find("#element").value.should == "You registered!"
end
# ... other tests like this
end
First question, how can I make sure I have 0 users before submitting the form?
Second, that before doesn’t quite seem right to me, even though it does the job. Is there another way of writing the tests for this whole registering use case? Perhaps reformulate the before part somehow, or is it acceptable the way I’m doing it?
UPDATE: I changed the before(:all) to before(:each) because tests started to fail. The body of the response seems to be incomplete when I’m using before(:all). Do you happen to know why?
You can can use the
change()matcher so as not to have to worry about what the count is beforehand:If you really want it to specifically go from 0 to 1, you can do this:
To answer the question about
before(:all)andbefore(:each),before(:each)will get called once before each example (itblock), whereasbefore(:all)gets called only once before all examples in thedescribeblock. This is whybefore(:all)is generally not recommended, since, if any of your examples change something about the initial state, later examples will be affected.