1
votes

I use Capybara and Cucumber to implement integration test for my website. However the data from database (sqlite) not showing in Capybara with "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

I tried to use rake db:seed RAILS_ENV=test to initial data for test environment successfully. But when I run rake cucumber then the data from this environment is empty. How I can fix to able test data from database (sqlite) with Capybara? Thanks

2

2 Answers

0
votes

By default, cucumber-rails runs DatabaseCleaner.start and DatabaseCleaner.clean before and after your scenarios. You can disable this behaviour like so:

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

instead of seeding data like this you should write a step in cucumber for this particular scenario for given data like,

  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