简体   繁体   English

水豚集成测试对象的创建

[英]Capybara integration test object creation

I have a basic integration test that is using Capybara, the problem is that if I do not create the required objects firstly the integration test fails. 我有一个使用Capybara的基本集成测试,问题是如果我不首先创建所需的对象,则集成测试将失败。 Am I required to create all objects as the first step in an integration test using Capybara? 作为使用Capybara进行集成测试的第一步,我是否需要创建所有对象? I am using Rails 4.2.4 with Capybara 2.4.3 我在Capybara 2.4.3使用Rails 4.2.4

Fail 失败

scenario 'if media content contains more than 10 items display pagination links' do 
  sign_in
  # Object creation
  11.times do 
    FactoryGirl.create(:media_content)
  end
  within '.pagination' do 
    expect(page).to have_content '1'
  end
end

Success 成功

scenario 'if media content contains more than 10 items display pagination links' do
  # Object creation
  11.times do 
    FactoryGirl.create(:media_content)
  end
  sign_in
  within '.pagination' do 
    expect(page).to have_content '1'
  end
end

If the objects creation affects the page that you are visit -ing in your capybara test then yes, you need to create the objects before you test for elements on that page because upon visiting the page, its content is already grabbed by the test browser. 如果对象的创建影响了您正在visit的页面-在水豚测试中,那么是的,您需要在测试该页面上的元素之前创建对象,因为访问该页面时,其内容已被测试浏览器捕获。

I assume that you have a visit "some_login_page" and perhaps a redirect upon successful login in your sign_in method, so when finishing the sign_in , the test browser already visited (ie grabbed) the page on which you are trying to test content later. 我假设您visit "some_login_page"并且可能在登录成功后使用sign_in方法进行了重定向,因此,在完成sign_in ,测试浏览器已经访问(即抓取了)您稍后要在其上测试内容的页面。

The only exception that comes to my mind is if you used a delayed AJAX request to grab the newly created elements from the server to the page dynamically - in that case creating the objects after page visit might work ok. 我想到的唯一例外是,如果您使用了延迟的AJAX请求来将新创建的元素从服务器动态地捕获到页面,则在这种情况下, 页面visit 之后创建对象可能就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins + Rspec + Capybara集成测试失败 - Jenkins + Rspec + Capybara integration test fail 如何使用rspec和capybara在RoR中运行集成测试 - How to run an integration test in RoR with rspec and capybara 使用Rails,Capybara和React组件进行集成测试 - Integration test with Rails, Capybara with React Component Rails 4:在Capybara和Poltergeist集成测试中存根 - Rails 4: stubbing in a Capybara & Poltergeist integration test 如何在Capybara集成测试中访问stdout或stderr - How to access stdout or stderr in a Capybara integration test 在Capybara集成测试(和Selenium Webdriver)过程中,设计用户创建失败 - Devise user creation fails during Capybara integration testing (and Selenium webdriver) 使用Capybara进行Rails集成测试时,将使用先前测试中的值填写表格 - Rails Integration Test With Capybara is Filling In Form with Values from Previous Test Capybara:两个rails应用程序之间的API集成测试 - Capybara : Integration test for an API between two rails apps 使用mocha和capybara在集成测试中设置用户实例的期望值 - setting expectation on user instance in integration test with mocha and capybara Rails:如何在我的Capybara集成测试中测试秒表? - Rails: how can I test a stopwatch in my Capybara integration tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM