开发者

RSpec doesn't remove DB record so it fails the second time it runs

开发者 https://www.devze.com 2023-03-11 17:09 出处:网络
This is from Michael Hartl\'s book, section 8.4.RSpec is testing a successful signup but is fails because the email address isn\'t unique. So if I go into the code and update the email address in the

This is from Michael Hartl's book, section 8.4. RSpec is testing a successful signup but is fails because the email address isn't unique. So if I go into the code and update the email address in the spec, it works the first time I run it but not the second time. I have confirmed this because I can make the test pass by changing the email address or otherwise running rake db:test:clone.

Any thoughts on how to overcome this would be appreciated.

Code: require 'spec_helper'

describe "Users" do
  describe "signup" do
    describe "failure" do
      it "should not make a new user" do
        lambda do
          visit signup_path
          fill_in :user_name,               :with => "" #you can use CSS id instead of label, which is probably better
          fill_in "Email",                  :with => ""
          fill_in "Password",               :with => ""
          fill_in "Password confirmation",  :with => ""
          click_button
          response.should render_template('users/new')
          response.should have_selector("div#error_explanation")
        end.should_not change(User, :count)
      end
    end
    describe "success" do
      it "should make a new user" do
        lambda do
          visit signup_path
          fill_in "Name",                   :with => "Example User"
          fill_in "Email",                  :with => "alex@example.com"
          fill_in "Password",               :with => "foobar"
          fill_in "Password confirmation",  :with => "foobar"
          click_button
          response.should have_selector("div.flash.success", :content => "Welcome")
          response.shoul开发者_StackOverflow中文版d render_template('users/show')
        end.should change(User, :count).by(1)
      end
    end 
  end
end


What does your spec/spec_helper.rb file look like? Do you have transactions turned on?

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end

This runs each of your specs within a database transaction, returning it back to its original state after each test run.

Once your spec helper looks something like that, run:

rake db:test:prepare

And try again. If that doesn't work, can you provide any more information? Which version of RSpec? Which version of Rails?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号