Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 使用分页后规范失败:每页_Ruby On Rails 4_Railstutorial.org_Specifications - Fatal编程技术网

Ruby on rails 4 使用分页后规范失败:每页

Ruby on rails 4 使用分页后规范失败:每页,ruby-on-rails-4,railstutorial.org,specifications,Ruby On Rails 4,Railstutorial.org,Specifications,我正在尝试显示应用程序中的用户列表。为此,我使用will_paginate gem。但是,当我试图更改页面上显示的用户数量时,尽管应用程序继续正常工作(我认为是这样),测试还是会失败 来自用户页面规范rb的一段代码 describe "pagination" do before(:all) { 30.times { FactoryGirl.create(:user) } } after(:all) { User.delete_all } it { should have_sel

我正在尝试显示应用程序中的用户列表。为此,我使用will_paginate gem。但是,当我试图更改页面上显示的用户数量时,尽管应用程序继续正常工作(我认为是这样),测试还是会失败

来自用户页面规范rb的一段代码

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.name)
    end
  end
end
FactoryGirl.define do
  factory :user do
    sequence(:name) { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
end
class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    #WANT TO CHANGE, DOESN'T WORK
    #@users = User.order('name ASC').paginate(:page => params[:page], :per_page => 8)

    #WORK
    #@users = User.order('name ASC').paginate :page => params[:page]

    #WORK
    #@users = User.paginate(:page => params[:page], :per_page => 30)

    #DOESN'T WORK
    @users = User.paginate(:page => params[:page], :per_page => 8)
  end
...
end
Failures:

  1) User pages index pagination should list each user
     Failure/Error: expect(page).to have_selector('li', text: user.name)
       expected #has_selector?("li", {:text=>"Person 38"}) to return true, got false
     # ./spec/requests/user_pages_spec.rb:26:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:25:in `block (4 levels) in <top (required)>'

Finished in 5.51 seconds
32 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:24 # User pages index pagination should list each user
来自工厂的代码。rb

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.name)
    end
  end
end
FactoryGirl.define do
  factory :user do
    sequence(:name) { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
end
class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    #WANT TO CHANGE, DOESN'T WORK
    #@users = User.order('name ASC').paginate(:page => params[:page], :per_page => 8)

    #WORK
    #@users = User.order('name ASC').paginate :page => params[:page]

    #WORK
    #@users = User.paginate(:page => params[:page], :per_page => 30)

    #DOESN'T WORK
    @users = User.paginate(:page => params[:page], :per_page => 8)
  end
...
end
Failures:

  1) User pages index pagination should list each user
     Failure/Error: expect(page).to have_selector('li', text: user.name)
       expected #has_selector?("li", {:text=>"Person 38"}) to return true, got false
     # ./spec/requests/user_pages_spec.rb:26:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:25:in `block (4 levels) in <top (required)>'

Finished in 5.51 seconds
32 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:24 # User pages index pagination should list each user
用户\u控制器.rb

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.name)
    end
  end
end
FactoryGirl.define do
  factory :user do
    sequence(:name) { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
end
class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    #WANT TO CHANGE, DOESN'T WORK
    #@users = User.order('name ASC').paginate(:page => params[:page], :per_page => 8)

    #WORK
    #@users = User.order('name ASC').paginate :page => params[:page]

    #WORK
    #@users = User.paginate(:page => params[:page], :per_page => 30)

    #DOESN'T WORK
    @users = User.paginate(:page => params[:page], :per_page => 8)
  end
...
end
Failures:

  1) User pages index pagination should list each user
     Failure/Error: expect(page).to have_selector('li', text: user.name)
       expected #has_selector?("li", {:text=>"Person 38"}) to return true, got false
     # ./spec/requests/user_pages_spec.rb:26:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:25:in `block (4 levels) in <top (required)>'

Finished in 5.51 seconds
32 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:24 # User pages index pagination should list each user
命令结果bundle exec rspec spec/requests/user\u pages\u spec.rb

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.name)
    end
  end
end
FactoryGirl.define do
  factory :user do
    sequence(:name) { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
end
class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    #WANT TO CHANGE, DOESN'T WORK
    #@users = User.order('name ASC').paginate(:page => params[:page], :per_page => 8)

    #WORK
    #@users = User.order('name ASC').paginate :page => params[:page]

    #WORK
    #@users = User.paginate(:page => params[:page], :per_page => 30)

    #DOESN'T WORK
    @users = User.paginate(:page => params[:page], :per_page => 8)
  end
...
end
Failures:

  1) User pages index pagination should list each user
     Failure/Error: expect(page).to have_selector('li', text: user.name)
       expected #has_selector?("li", {:text=>"Person 38"}) to return true, got false
     # ./spec/requests/user_pages_spec.rb:26:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:25:in `block (4 levels) in <top (required)>'

Finished in 5.51 seconds
32 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:24 # User pages index pagination should list each user
故障:
1) 用户页面索引分页应列出每个用户
失败/错误:期望(第页)有_选择器('li',文本:user.name)
期望#has_selector?((“li”,{:text=>“Person 38”})返回true,得到false
#./spec/requests/user\u pages\u spec.rb:26:in'block(5级)in'
#./spec/requests/user\u pages\u spec.rb:25:in'block(4级)in'
以5.51秒完成
32例,1例失败
失败的示例:
rspec./spec/requests/user_pages_spec.rb:24#用户页面索引分页应列出每个用户

它仅在用户页面规范rb和用户控制器中工作。rb是数字30(将分页的默认值)。就在这个测试中,其他人都通过了。一点帮助就好了。

在rspec spec/requests/user\u pages\u spec.rb上添加相同的更改

User.paginate(page: 1, :per_page => 8).each do |user|

在rspec spec/requests/user\u pages\u spec.rb上添加相同的更改

User.paginate(page: 1, :per_page => 8).each do |user|