2016-11-06 237 views
1

这里真的很奇怪的问题。当我孤立地运行我的测试时,我的3个测试都通过了。但是,如果我运行上下文,它们都会失败,但会间歇性地通过。测试正在彼此碰撞,但我不知道在哪里或如何。以下是错误的测试。测试通过隔离,但运行套件时失败 - RSpec

context "creating an account" do 
    let!(:user) { login_user } 
    let!(:plan) { create(:plan, :public, :unlimited, name: "Private Eye") } 

    before do 
     allow_any_instance_of(Gabba::Gabba).to receive_messages(event: true) 
    end 

    it "can create a new account" do 
     visit new_account_path 

     fill_in :account_name, with: "New Account" 

     find("#stripe_card_token").set("tok_3HUgs79WEx47q9") 

     VCR.use_cassette("stripe/create_customer_paid_plan") do 
     choose("plan_id_2") 
     fill_in :card_number, with: "4242424242424242" 
     select "12", from: :date_month 
     select Date.current.year, from: :date_year 

     expect { click_button "Complete Project Setup" } 
      .to change(Account, :count).by(1) 
      .and change(user.reload.accounts, :count).by(1) 
     end 

     account = Account.find_by(name: "New Account") 

     expect(current_path).to eq(snitches_path) 
     expect(account.name).to eq("New Account") 
     expect(account.cc_last4).to eq("4242") 
     expect(account.cc_expiration_year).to eq(2016) 
     expect(account.cc_expiration_month).to eq(12) 
    end 

    it "for a paid plan without a credit card" do 
     visit new_account_path 

     choose("plan_id_2") 
     fill_in :account_name, with: "My New Account" 

     expect { click_button "Complete Project Setup" } 
     .not_to change(Account, :count) 

     expect(page).to have_content("Oops. You need to enter a credit card to sign up for a paid plan.") 
    end 

    it "switches user to the new account upon creation" do 
     visit new_account_path 

     find("#stripe_card_token").set("tok_3HUgs79WEx47q9") 

     VCR.use_cassette("stripe/create_customer_paid_plan") do 
     choose("plan_id_2") 
     fill_in :account_name, with: "My New Account" 
     fill_in :card_number, with: "4242424242424242" 
     select "12", from: :date_month 
     select Date.current.year, from: :date_year 

     click_button "Complete Project Setup" 
     end 

     expect(current_path).to eq(snitches_path) 
     expect(page).to have_content("My New Account") 
     expect(Account.find_by(id: current_account).name).to eq("My New Account") 
    end 
    end 

也许你会看到蝙蝠的东西,但我似乎无法找到问题。如果有人有想法,为什么会发生这种情况,我会喜欢它!谢谢。

只是要清楚。如果我孤立地运行其中的任何一个,如果我运行上下文,它们每个都会通过但失败。

回答

1

您可以使用bisect命令来识别使测试失败的执行顺序。

的命令是:

rspec --bisect 

它返回是这样的:

The minimal reproduction command is: 
    rspec ./spec/features/my_amazing_feature_spec.rb[1:1] 

所以,你可以用它来修复您的代码并确认修复。

相关问题