为什么这个规格/控制器测试不会通过?它之前做过......(Rails,RSpec)

时间:2011-07-25 20:50:14

标签: ruby-on-rails-3 testing rspec rspec-rails rails-postgresql

以下是我的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)>

协助表示赞赏!

1 个答案:

答案 0 :(得分:1)

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