1

I'm writing integration tests for creating a user account.

describe "with valid information" do
  before (:each) do
    fill_in 'first name',             :with => 'test'
    fill_in 'last name',              :with => 'user'
    fill_in "email",                  :with => "test@shakeshack.com"
    fill_in "password",               :with => "1234567"
    fill_in "password confirmation",  :with => "1234567"
  end

  it "should create a user" do
    expect {click_button "Create"}.to change(User, :count).by(1)

  end
end

when I run a local server and fill in the above fields with the same information, the user account is created successfully. The error I am getting is as follows:

User pages signup with valid information should create a user
 Failure/Error: expect {click_button "Create"}.to change(User, :count).by(1)
   count should have been changed by 1, but was changed by 0
 # ./spec/requests/users_spec.rb:43:in `block (4 levels) in <top (required)>'

thanks in advance for solutions or suggestions!!

4

1 に答える 1

3

A couple of suggestions and things to look for:

  • Are you sure the User was created successfully when you tested in the browser? Did you verify in the Rails console or some other way?
  • Are you sure this is enough data to successfully create a user?
  • What usually happens to me in a case like this is a silent failure somewhere, perhaps as the result of not using the bang methods (e.g. save!) or not properly checking the return value of the non-bang methods (e.g. save).

You can always use capybara's save_and_open_page in the spec to view a rendering of the page in the browser during the test:

describe "with valid information" do
  before (:each) do
    fill_in 'first name',             :with => 'test'
    fill_in 'last name',              :with => 'user'
    fill_in "email",                  :with => "test@shakeshack.com"
    fill_in "password",               :with => "1234567"
    fill_in "password confirmation",  :with => "1234567"
    save_and_open_page
  end

  it "should create a user" do
    expect {click_button "Create"}.to change(User, :count).by(1)

  end
end
于 2013-01-12T21:12:12.240 に答える