Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec绕过测试中的独特需求_Ruby On Rails_Ruby On Rails 4_Rspec_Capybara_Rspec Rails - Fatal编程技术网

Ruby on rails RSpec绕过测试中的独特需求

Ruby on rails RSpec绕过测试中的独特需求,ruby-on-rails,ruby-on-rails-4,rspec,capybara,rspec-rails,Ruby On Rails,Ruby On Rails 4,Rspec,Capybara,Rspec Rails,我的注册数据库在电子邮件上有一个索引,有一个独特的要求。这很好,但问题是我正在尝试运行集成测试,每次我去rake-rspec spec/features/…rb,除非我做了rake-db:test:purge,rake-db:test:prepare,否则它首先会遇到唯一的问题并拒绝运行。我如何简化这一点 从下面的代码中,您可以看到,每次运行测试时,我都使用(:all)之前的创建一组种子数据,但由于种子数据总是相同的,这导致了唯一性错误 只要我的测试套件仍然能够使用该种子数据运行,我很乐意将该种

我的注册数据库在电子邮件上有一个索引,有一个独特的要求。这很好,但问题是我正在尝试运行集成测试,每次我去
rake-rspec spec/features/…rb
,除非我做了
rake-db:test:purge
rake-db:test:prepare
,否则它首先会遇到唯一的问题并拒绝运行。我如何简化这一点

从下面的代码中,您可以看到,每次运行测试时,我都使用(:all)之前的
创建一组种子数据,但由于种子数据总是相同的,这导致了唯一性错误

只要我的测试套件仍然能够使用该种子数据运行,我很乐意将该种子数据放在其他地方或以其他方式创建它

describe "how requests should flow" do

    before(:all) do 
        @signup_dd = Signup.create(email:"example@example.com")
    end

    it "should have 2 inventories and 2 signups to start" do
        Signup.count.should == 1
    end

    describe "request creation" do
        before do
            Signup.find_by_id(@signup_dd)
            visit '/requests/new'
            save_and_open_page
            fill_in '#borrow__1', :with => 1
            click_button
        end
        it "should affect new Requests and Borrows" do
            ...
        end
    end
end

有两种方法可以解决此问题:

  • 之前的
    块中删除
    (:all)
    。RSpec将为每个测试执行before块。然后,它将在每次测试后撤消自身。这是您真正想要的,因为它确保了每个测试所做的更改不会渗透到其他测试中。这通常是推荐的方法

  • 保留
    (:all)
    ,然后添加
    (:after)
    块以撤消更改。使用
    :all
    参数,before块只执行一次,而不是每次执行一次。但是,它不会像
    :each
    那样自动撤消自身,因此需要
    :after
    块。然而,这取决于你,去弄清楚什么需要进入那里。例如,在您的示例中,它可能是:

    after(:all) do
        Signup.delete_all # Or whatever undoes what the before block did
    end
    

  • 请参阅。

    当您在(:all)
    之前使用
    时,您需要在(:all)
    之后使用
    来清理您在(:all)
    之前(:all)

    中创建的数据,如果您只在(
    )之前编写
    ,而不使用
    (:all)
    ,会发生什么?哦,是的,这看起来很有效!好的,这是对其他有同样问题的人的回答,这样就不会一直没有答案了。