如果您使用的是RSPEC,肯定應該是「改變」的方法。這裏有兩個例子消極和積極的一個,這樣你可以有語法感:
RSpec.describe "UsersSignups", type: :request do
describe "signing up with invalid information" do
it "should not work and should go back to the signup form" do
get signup_path
expect do
post users_path, user: {
first_name: "",
last_name: "miki",
email: "[email protected]",
password: "buajaja",
password_confirmation: "juababa"
}
end.to_not change{ User.count }
expect(response).to render_template(:new)
expect(response.body).to include('errors')
end
end
describe "signing up with valid information" do
it "should work and should redirect to user's show view" do
get signup_path
expect do
post_via_redirect users_path, user: {
first_name: "Julito",
last_name: "Triculi",
email: "[email protected]",
password: "worldtriculi",
password_confirmation: "worldtriculi"
}
end.to change{ User.count }.from(0).to(1)
expect(response).to render_template(:show)
expect(flash[:success]).to_not be(nil)
end
end
它看起來像你使用使用RSpec的一個assert_difference寶石,是否正確?我想你需要把它包裝在一個'it'塊中。 – 2012-03-15 03:53:13
我試圖把它放在塊中,但仍然有這個錯誤 – 2012-03-15 06:54:59