【问题标题】:Data from database (SQLite) not showing in Capybara来自数据库(SQLite)的数据未在 Capybara 中显示
【发布时间】:2013-09-05 06:32:29
【问题描述】:

我使用 Capybara 和 Cucumber 为我的网站实施集成测试。但是来自数据库(sqlite)的数据没有显示在 Capybara 中 "print page.html"

index.html.erb:

<% provide(:title, "All posts")%>

<h3>Blogs <%= link_to "Create a post", new_post_path, class: 'pull-right' %></h3>

<%= will_paginate @posts %>
<%= render @posts %>
<%= will_paginate @posts %>

posts_controller.rb:

class PostsController < ApplicationController
    def index
       @posts = Post.paginate(page: params[:page], per_page: 10)
    end
end

我尝试成功使用 rake db:seed RAILS_ENV=test 来测试环境的初始数据。但是当我运行rake cucumber 时,这个环境中的数据是空的。如何使用 Capybara 修复数据库(sqlite)中的测试数据?谢谢

【问题讨论】:

    标签: ruby-on-rails ruby sqlite cucumber capybara


    【解决方案1】:

    默认情况下,cucumber-rails 在您的场景之前和之后运行 DatabaseCleaner.startDatabaseCleaner.clean。您可以像这样禁用此行为:

      # features/support/env.rb
      # ...
      Cucumber::Rails::Database.autorun_database_cleaner = false
    

    【讨论】:

      【解决方案2】:

      而不是像这样播种数据,您应该为这种特定场景在黄瓜中为给定数据编写一个步骤,例如,

        Scenario: Post index
          Given I have 10 posts
          When I go to the list of posts
          ......
      
      Given /^I have (.+) posts$/ do |num|
        num.to_i.times do
          Post.create(title: "post_#{num}")
        end
      end
      

      【讨论】:

        猜你喜欢
        • 2017-09-20
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 1970-01-01
        • 2013-10-06
        • 2015-04-11
        • 2012-10-16
        • 1970-01-01
        相关资源
        最近更新 更多