在我的仪表板页面上,我有一个Metrics部分,其中显示了用户的目标数。对于没有目标的用户,我不显示本节。当用户创建目标和重定向之后,Metrics部分将出现。
在下面的RSpec测试中,当RSpec首先随机运行第一个describe
时,测试会通过,因为它找不到Metrics部分。但是,当RSpec首先运行第二个describe
块时,第一个describe
块会失败,因为此时重定向已经发生,并且Metrics部分已经出现。
如何确保每个块单独运行并通过?
describe "Dashboard Pages", :type => :request do
subject { page }
let(:user) { FactoryGirl.create(:user) }
before(:each) do
sign_in user
end
describe "After user signs in - No Goals added yet" do
it { is_expected.to have_title(full_title('Dashboard')) }
it { is_expected.to have_content('Signed in successfully')}
it "should not show the metrics section" do
expect(page).to_not have_css("div#metrics")
end
end
#
#Notice that this runs using the SELENIUM WebDriver
#
describe "After user signs in - Add a new Goal" do
it "should display the correct metrics in the dashboard", js: true do
click_link "Create Goal"
fill_in "Goal Name", :with=> "Goal - 1"
fill_in "Type a short text describing this goal:", :with => "A random goal!"
click_button "Save Goal"
end
end
end
发布于 2016-04-02 04:55:53
我认为您的问题是,click_button "Save Goal"
发送的请求在测试完成后到达服务器。Capybara的Javascript驱动程序是异步的,不需要等待它们发送给浏览器的命令完成。
让Capybara等待的通常方法是,当您想要等待的命令完成时,期望页面上的内容是真实的。无论如何,这是一个好主意,因为上一次测试实际上并不期望指标像它所说的那样显示。因此,预期它们是:
it "should display the correct metrics in the dashboard", js: true do
click_link "Create Goal"
fill_in "Goal Name", :with=> "Goal - 1"
fill_in "Type a short text describing this goal:", :with => "A random goal!"
click_button "Save Goal"
expect(page).to have_css("div#metrics")
end
另外,请注意,当前的RSpec和Capybara不允许您在请求规范中使用Capybara。除非您由于其他原因而绑定到旧版本,否则我建议升级到当前的RSpec和Capybara,并将您的请求规范转换为特性规范。
https://stackoverflow.com/questions/36364123
复制