Rspec / Capybara测试用例不适用于多个'it'块

时间:2013-11-07 01:12:45

标签: 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 "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”块,如下所示

 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"

还有一件事,我必须测试远程(url:http://groups.caremonkey.com/)中的所有功能。因为,我正在为现有应用程序编写集成测试。另外,在测试我的应用程序的其余功能之前,我需要登录系统。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您是否遵循了calling remote servers的Capybara文档?它说你应该有以下内容:

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次呼叫会尝试使用相对路由,然后将其路由到默认服务器。如果你没有运行某种Rack服务器,我想不出为什么你会得到ActionController::RoutingError。你在其他一些Rails应用程序中运行这些测试吗?

答案 1 :(得分:0)

我猜是这样的:

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").click_link("Add")所以我把它添加到前一个块然而这个测试是神秘的,什么是第一个(“。actual img”)?