Ruby on rails Rspec/Capybara测试用例不适用于多个';它';阻碍
我正在为现有应用程序编写一些集成测试用例。如果只有一个“it”块,我的测试工作正常。但是,如果我添加多个“it”块,它将抛出一个错误。下面是我的代码:Ruby on rails Rspec/Capybara测试用例不适用于多个';它';阻碍,ruby-on-rails,ruby,rspec,capybara,integration-testing,Ruby On Rails,Ruby,Rspec,Capybara,Integration Testing,我正在为现有应用程序编写一些集成测试用例。如果只有一个“it”块,我的测试工作正常。但是,如果我添加多个“it”块,它将抛出一个错误。下面是我的代码: require 'spec_helper' describe 'Group' do before do visit 'http://groups.caremonkey.com/users/sign_in' fill_in "Email", :with => "email@example.com" fill_in
require 'spec_helper'
describe 'Group' do
before do
visit 'http://groups.caremonkey.com/users/sign_in'
fill_in "Email", :with => "email@example.com"
fill_in "Password", :with => "password"
click_button "Login"
page.should have_link('Account')
end
it 'Should check all the links and functionality of groups' do
#add new subgroup with valid data should save a new group
find("#group-squares").click_link("Add")
fill_in "Group Name", :with => "Melbourne futsal"
click_on("Save")
page.should_not have_content("can't be blank")
page.execute_script("parent.$.fancybox.close();")
page.should have_link('Account')
#test edit group: should be able to update group info provided valid data are given
first(".actual img").click
page.should have_content("Group")
page.should have_link("Cancel")
fill_in "Group name", :with => "Futsal club"
page.execute_script("$('#sub-group-color-options').find('.color23').click()")
click_button "Save"
click_on("Cancel")
page.should have_link('Account')
end
end
当我把所有的“It”块放在一个“It”块中时,它工作得非常好。但当我把它们分成不同的“it”块时,它就停止工作了。例如,如果我将此(“测试编辑组:应该能够更新组信息,前提是提供了有效数据”)测试用例拆分为单独的“it”块,如下所示
require 'spec_helper'
describe 'Group' do
before do
visit 'http://groups.caremonkey.com/users/sign_in'
fill_in "Email", :with => "email@example.com"
fill_in "Password", :with => "password"
click_button "Login"
page.should have_link('Account')
end
it 'add new subgroup with valid data should save a new group' do
find("#group-squares").click_link("Add")
fill_in "Group Name", :with => "Melbourne futsal"
click_on("Save")
page.should_not have_content("can't be blank")
page.execute_script("parent.$.fancybox.close();")
page.should have_link('Account')
end
it 'should be able to update group info provided valid data are given' do
first(".actual img").click
page.should have_content("Group")
page.should have_link("Cancel")
fill_in "Group name", :with => "Futsal club"
page.execute_script("$('#sub-group-color-options').find('.color23').click()")
click_button "Save"
click_on("Cancel")
page.should have_link('Account')
end
end
然后rspec失败,它通过了第一个测试,但是第二个测试失败,抛出了以下错误
Failure/Error: visit 'http://groups.caremonkey.com/users/sign_in'
ActionController::RoutingError:
No route matches [GET] "/users/sign_in"
还有一件事,我必须测试remote(url:)中的所有功能。因为,我正在为现有应用程序编写集成测试。此外,在测试应用程序的其余功能之前,我需要登录到系统。提前感谢您的帮助。我猜是这样的:
require 'spec_helper'
describe 'Group' do
before do
visit 'http://groups.caremonkey.com/users/sign_in'
fill_in "Email", :with => "email@example.com"
fill_in "Password", :with => "password"
click_button "Login"
page.should have_link('Account')
find("#group-squares").click_link("Add") #apperently both specs are "scoped" to this page
end
it 'Should check all the links and functionality of groups' do
fill_in "Group Name", :with => "Melbourne futsal"
click_on("Save")
page.should_not have_content("can't be blank")
page.execute_script("parent.$.fancybox.close();")
page.should have_link('Account')
end
it "test edit group: should be able to update group info provided valid data are given"
first(".actual img").click
page.should have_content("Group")
page.should have_link("Cancel")
fill_in "Group name", :with => "Futsal club"
page.execute_script("$('#sub-group-color-options').find('.color23').click()")
click_button "Save"
click_on("Cancel")
page.should have_link('Account')
end
end
我的直觉告诉我这两个测试都需要遵循以下步骤:
find(“#group squares”)。单击链接(“Add”)
,所以我将它添加到before block中。但是这个测试很神秘,什么是第一个(.actual img”)?您是否遵循了水豚的文档?它说你应该具备以下条件:
Capybara.current_driver = :selenium # Or anything but rack_test, probably
Capybara.run_server = false # Don't run your app in-process
Capybara.app_host = 'http://groups.caremonkey.com/'
我的猜测是,当您访问过一次站点时,将来的
visit
调用将尝试使用相对路由,然后将其路由到默认服务器。我想不出如果没有某种机架式服务器运行,为什么会出现ActionController::RoutingError
。您是否在其他Rails应用程序中运行这些测试?您是否打算进行bash@caremonkey.com
用户在互联网上完全公开?不,这不是故意的。我更改了我的用户/通行证只是为了测试。感谢您的关注。请更新您的问题,以准确显示您是如何将测试拆分的,以及错误消息是什么。@Shepmaster:我已经用错误消息更新了我的问题。你现在可以看看了。谢谢你的回答。然而,这对我来说不起作用<代码>查找(“#组方块”)。单击链接(“添加”)是测试组创建的链接,而是第一个(“.actual img”)。单击
是指组的第一个编辑链接。Rspec在第二次尝试在块之前执行时抛出路由错误。我也这样做了。。这也没用。无论如何谢谢你!