【发布时间】:2013-08-23 00:37:11
【问题描述】:
所以我在这里使用 Rails 4 制作一个网络应用程序,如果它有帮助的话,我会在 Mac 上使用 Mountain Lion,使用 Aptana Studio 3 进行开发。
我正在尝试创建一个页面来列出和分页我的应用程序的用户。我正在使用 will_paginate 和 bootstrap-will_paginate 来做到这一点。当我在我的数据库上使用示例用户运行 rails 服务器时,它运行良好,但是当我使用 RSpec 和 FactoryGirl 运行我的测试时,我对需要用户授权的页面的所有测试都不起作用。所以这就是我认为问题出在 FactoryGirl 的原因。
以前,我的 factory.rb 看起来像这样,因为它只需要创建一个用户:
FactoryGirl.define do
factory :user do
username "example_user"
password "foobar"
password_confirmation "foobar"
end
end
现在我将其更改为:
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "Person_#{n}" }
password "foobar"
password_confirmation "foobar"
end
end
这里是对我的 user_pages_spec 文件的更改。在测试停止通过之前,除了工厂文件之外,这是我唯一更改的内容。这是它的差异:
describe "index" do
- before do
- sign_in FactoryGirl.create(:user)
- FactoryGirl.create(:user, username: "bananarama")
- FactoryGirl.create(:user, username: "appleface")
+ let(:user) { FactoryGirl.create(:user) }
+ before(:each) do
+ sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
- it "should list each user" do
- User.all.each do |user|
- expect(page).to have_selector('li', text: user.username)
+ describe "pagination" do
+
+ before(:all) { 30.times { FactoryGirl.create(:user) } }
+ after(:all) { User.delete_all }
+
+ it { should have_selector('div.pagination') }
+
+ it "should list each user" do
+ User.paginate(page: 1).each do |user|
+ expect(page).to have_selector('li', text: user.username)
+ end
end
end
end
有什么遗漏吗?还有什么我应该看的吗?同样,失败的测试是关于授权页面上的测试。这些包括我的 authorization_pages_spec.rb 中的一些
非常感谢!
【问题讨论】:
标签: ruby-on-rails ruby rspec factory-bot