Using Let to build multiple instances of a model
capybara, factory-bot, rspec, ruby, ruby-on-rails
Solution
Your example has a few issues. As mentioned by others `let` is lazily evaluated, so to create objects that aren't directly referenced you would need to use `let!`. Additionally, FactoryGirl's `create` doesn't take a number of records to produce, you need `create_list` for that. Finally, Capybara's `have_link` takes the text of the link you're looking for as the first parameter so there's no need to pass a `:text` option
RSpec.describe "Users Index", type: :feature do
describe "Pagination" do
let!(:valid_user) { create(:user, name: "Mogli") }
let!(:other_users) { create_list(:user, 50) }
it "successfully paginates" do
log_in_as_feature(valid_user)
visit users_path
puts URI.parse(current_url)
expect(page).to have_css('div.pagination')
expect(page).to have_link(valid_user.name, href: user_path(valid_user))
User.paginate(page: 1).each do |user|
expect(page).to have_link(user.name, href: user_path(user))
expect(page).to have_link("delete", href: user_path(user)) unless user == valid_user
end
end
end
Problem
I'm trying to run a test on will_paginate. I know that it technically works, but I can't get the spec to work because of my inability to create multiple records. I'm using Capybara and Rspec on with Ruby on Rails. Here is what I have in my feature spec. ``` RSpec.describe "Users Index", type: :feature do describe "Pagination" do let(:valid_user) { create(:user, name: "Mogli") } let(:other_user) { create(:user, 50) } it "successfully paginates" do log_in_as_feature(valid_user) visit users_path puts URI.parse(current_url) expect(page).to have_css('div.pagination') expect(page).to have_link(href: user_path(valid_user), text: valid_user.name) first_page_of_users = User.paginate(page: 1) first_page_of_users.each do |user| expect(page).to have_link(href: user_path(user), text: user.name) unless user == valid_user expect(page).to have_link(href: user_path(user), text: "delete") end end end end ``` end My factory is simple: ``` FactoryGirl.define do sequence(:name) { |n| "Person#{n}" } factory :user do name email { "#{name}@example.com" } password "foobar" password_confirmation "foobar" activated true activated_at Time.zone.now end end ``` My fourth line is the culprit. It's not actually building the users. I tried to use a `FactoryGirl.create(:user, 50)`, but that ends up breaking 27 tests across the board, and I have to reset the test database. I don't know how else to create more than one dummy user at once, all the while keeping Mogli as first. Any insight is appreciated. Edit: If I commented the have_css test, then my tests pass. Here is the error of the div `1) Users Index Pagination successfully paginates Failure/Error: expect(page).to have_css('div.pagination') expected to find css "div.pagination" but there were no matches # ./spec/features/users_index_spec.rb:13:in`block (3 levels) in ' Finished in 0.82368 seconds (files took 2.17 seconds to load)` EDIT: adding my partial and index.html.erb view. My view just renders @users partial which is: ``` 1 <li> 2 <%= gravatar_for user, size: 50 %> 3 <%= link_to user.name, user %> 4 <% if current_user.admin? && !current_user?(user) %> 5 | <%= link_to "delete", user, method: :delete, 6 data: { confirm: "You sure?" } %> 7 <% end %> 8 9 </li> ```