Ruby on rails RSpec不';t删除数据库记录,使其在第二次运行时失败

Ruby on rails RSpec不';t删除数据库记录,使其在第二次运行时失败,ruby-on-rails,rspec,Ruby On Rails,Rspec,这是迈克尔·哈特尔的书,第8.4节。RSpec正在测试成功的注册,但由于电子邮件地址不唯一而失败。因此,如果我进入代码并更新规范中的电子邮件地址,它会在我第一次运行它时起作用,但不会在第二次运行时起作用。我已经确认了这一点,因为我可以通过更改电子邮件地址或运行rake db:test:clone来通过测试 任何关于如何克服这一问题的想法都将不胜感激 代码: 需要“spec\u helper” describe "Users" do describe "signup" do descr

这是迈克尔·哈特尔的书,第8.4节。RSpec正在测试成功的注册,但由于电子邮件地址不唯一而失败。因此,如果我进入代码并更新规范中的电子邮件地址,它会在我第一次运行它时起作用,但不会在第二次运行时起作用。我已经确认了这一点,因为我可以通过更改电子邮件地址或运行rake db:test:clone来通过测试

任何关于如何克服这一问题的想法都将不胜感激

代码: 需要“spec\u 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.should render_template('users/show')
        end.should change(User, :count).by(1)
      end
    end 
  end
end

spec/spec\u helper.rb文件是什么样子的?您是否打开了事务

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end
这将在数据库事务中运行每个规范,并在每次测试运行后将其返回到原始状态

一旦您的等级库助手看起来像这样,请运行:

rake db:test:prepare

然后再试一次。如果这不起作用,你能提供更多的信息吗?哪个版本的RSpec?Rails的哪个版本?

您的spec/spec\u helper.rb文件看起来像什么?您是否打开了事务

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end
这将在数据库事务中运行每个规范,并在每次测试运行后将其返回到原始状态

一旦您的等级库助手看起来像这样,请运行:

rake db:test:prepare

然后再试一次。如果这不起作用,你能提供更多的信息吗?哪个版本的RSpec?哪个版本的Rails?

谢谢。我使用的是Rails 3.0.1和RSpec 2.5.1。实际上我只是检查了一下,我的测试到处都不及格。我签出了spec_helper.rb,它已经将该标志设置为true:“config.use_transactional_fixtures=true”Croak。。。我刚刚运行了rakedb:test:prepare,现在一切都运行得很顺利。谢谢@亚历克斯的评论很有帮助。我在config.use_transactional_fixtures=true时也遇到了同样的错误,同样的更改也解决了这个问题。谢谢。我使用的是Rails 3.0.1和RSpec 2.5.1。实际上我只是检查了一下,我的测试到处都不及格。我签出了spec_helper.rb,它已经将该标志设置为true:“config.use_transactional_fixtures=true”Croak。。。我刚刚运行了rakedb:test:prepare,现在一切都运行得很顺利。谢谢@亚历克斯的评论很有帮助。我在config.use\u transactional\u fixtures=true时也遇到了同样的错误,同样的更改修复了它。