为什么此规范/控制器测试无法通过?以前确实...(导轨,RSpec)



下面是我的spec/controllers/superadmin/users_controller_spec.rb:

  describe "GET index" do
    it "receives the where action twice" do
      User.should_receive(:where).twice
      get :index
    end
    it "assigns @superadmins" do
      get :index
      assigns[:superadmins].should_not be_nil
    end
    it "assigns @admins" do
      get :index
      assigns[:admins].should_not be_nil
    end
  end

下面是我的app/controllers/superadmin/users_controller.rb:

class Superadmin::UsersController < SuperadminController
  def index
    @superadmins = User.where(:role => 'superadmin')
    @admins = User.where(:role => 'admin')
  end
  ...
end

如果我没记错的话,这个测试过去是通过的。自从我将PostgreSQL设置为测试和开发环境的数据库以来,这个测试一直失败…不知道为什么。

错误信息:

Failures:
  1) Superadmin::UsersController GET index receives the where action twice
     Failure/Error: User.should_receive(:where).twice
       (<User(id: integer, role: string, first_name: string, last_name: string, login: string, email: string, crypted_password: string, password_salt: string, persistence_token: string, single_access_token: string, perishable_token: string, login_count: integer, failed_login_count: integer, last_request_at: datetime, current_login_at: datetime, last_login_at: datetime, current_login_ip: string, last_login_ip: string, created_at: datetime, updated_at: datetime, restaurant_id: integer, organization_id: integer) (class)>).where(any args)
           expected: 2 times
           received: 0 times
     # ./spec/controllers/superadmin/users_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
  2) Superadmin::UsersController GET index assigns @superadmins
     Failure/Error: assigns[:superadmins].should_not be_nil
       expected: not nil
            got: nil
     # ./spec/controllers/superadmin/users_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
  3) Superadmin::UsersController GET index assigns @admins
     Failure/Error: assigns[:admins].should_not be_nil
       expected: not nil
            got: nil
     # ./spec/controllers/superadmin/users_controller_spec.rb:17:in `block (3 levels) in <top (required)>

援助赞赏!

我在您提供的代码中没有看到任何会使这些测试失败的内容。你有任何可能失败的before_filters吗?我猜控制器需要身份验证。你在测试中设置了before块吗?

相关内容

  • 没有找到相关文章

最新更新