0

以下是我的规范/控制器/超级管理员/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)>

协助表示赞赏!

4

1 回答 1

1

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

于 2011-07-29T15:49:28.683 回答