【发布时间】:2023-04-06 19:50:02
【问题描述】:
我正在尝试对 will_paginate 进行测试。我知道它在技术上是可行的,但由于我无法创建多个记录,我无法让规范正常工作。我在 Ruby on Rails 上使用 Capybara 和 Rspec。
这是我的功能规范中的内容。
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
结束
我的工厂很简单:
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
我的第四行是罪魁祸首。它实际上并没有建立用户。我尝试使用FactoryGirl.create(:user, 50),但这最终会破坏 27 个测试,我必须重置测试数据库。
我不知道如何同时创建多个虚拟用户,同时保持 Mogli 为第一。任何见解都值得赞赏。
编辑:如果我评论了 have_css 测试,那么我的测试通过了。
这里是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:inblock (3 个级别) in '
在 0.82368 秒内完成(文件加载需要 2.17 秒)`
编辑:添加我的部分视图和 index.html.erb 视图。
我的视图只是呈现@users 部分
这是:
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>
【问题讨论】:
-
我认为
let(:other_user) { create(:user, 50) }应该像let(:other_user) { create(:user, name: 'another name')}` -
您刚刚在问题中更新的错误意味着 div 在页面上不可见。查看
page.html并查看页面上的实际内容。
标签: ruby-on-rails ruby rspec capybara factory-bot