Ruby on rails Rspec测试持续失败,预期。。。一无所获

Ruby on rails Rspec测试持续失败,预期。。。一无所获,ruby-on-rails,rspec,Ruby On Rails,Rspec,我正在尝试进入TDD,目前我正在努力处理此错误消息: UsersController GET 'show' should find the right user Failure/Error: expect(user).to eq(@user) expected: #<User id: 1, email: "example@example.com", encrypted_password: "$2a$04$AVGGS0XU1Kjmbdc/iZ86iOq2f4k992

我正在尝试进入TDD,目前我正在努力处理此错误消息:

UsersController GET 'show' should find the right user
     Failure/Error: expect(user).to eq(@user)

       expected: #<User id: 1, email: "example@example.com", encrypted_password: "$2a$04$AVGGS0XU1Kjmbdc/iZ86iOq2f4k992boP7xfqcg2nl6...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2014-06-08 19:43:41", updated_at: "2014-06-08 19:43:41", name: "Test User", confirmation_token: nil, confirmed_at: "2014-06-08 19:43:41", confirmation_sent_at: nil, unconfirmed_email: nil, account_id: 1, notify: nil>
            got: nil

       (compared using ==)
     # ./spec/controllers/users_controller_spec.rb:28:in `block (3 levels) in <top (required)>'
这是控制器:

class UsersController < ApplicationController
...
    def show
        authorize! :show, @user, :message => 'Not authorized as an administrator.'
        @user = current_account.users.find(params[:id])
    end

...
end

我怎样才能避开这个错误消息?我感觉它与before_filter有关,但这只是一个猜测。

在用户控制器上是否有before_filter,您需要验证才能使用show方法?您的before块进行两次调用,创建一个用户,然后“登录”,某种身份验证。但是您的测试只是查看用户是否已创建,show方法是否返回正确的用户记录以及传递的id

所以我会打破你之前的障碍。首先测试工厂是否已成功创建:

it 'has a valid factory' do
    FactoryGirl.create(:user).should eq(true) # or should be_valid.. 
end
如果通过了,我将注释掉您的用户控制器(暂时)上sign_-in方法的任何约束,并测试show是否接收到id并返回正确的用户


如果通过,则在用户控制器上重新添加约束以进行验证,并找出如何测试登录方法。:)

好的,所以我通过当前未设置的\u帐户进行调试,找到了答案。我找到了这个答案

我想我在最初的问题中遗漏了一个主要的谜题,所以我对此表示歉意,因为我不知道这会影响我的RSPEC

诀窍在于

before(:each) do
  @account = FactoryGirl.create(:account)
  @request.host = "#{@account.subdomain}.example.com"
end

FactoryGirl.create是否将用户添加到用户表中?else current_account.users.find(params[:id])不起作用。只是猜测我刚刚在问题中添加了用户工厂。使用调试器或添加日志来查看第二行
show
没有设置用户的原因。查看
当前账户
当前账户的值。用户
参数[:id]
@current\u账户
包含与我期望的相同的
用户
。当我执行
put params=#{params]
时,我得到了以下错误:
失败/错误:将“params=#{params}”名称错误:如果我将
put params=#{params]
更改为
put params=#{controller.params]
我看到一些参数。尽管测试仍然失败。那么我会将
有一个有效的工厂
测试放在我的前块还是后块中?如果我将它放在后块中,我会收到
电子邮件,电子邮件已经被接收了
。此外,登录方法是Desive的登录方法。
有一个有效的工厂
测试通过得很好。我已经通过了尝试注释掉我的应用程序控制器中的所有before_过滤器和所有can授权检查,但仍然不起作用。
FactoryGirl.define do
  factory :user do
    name 'Test User'
    account_id 1
    email 'example@example.com'
    password 'changeme'
    password_confirmation 'changeme'
    # required if the Devise Confirmable module is used
    confirmed_at Time.now
  end
end
it 'has a valid factory' do
    FactoryGirl.create(:user).should eq(true) # or should be_valid.. 
end
before(:each) do
  @account = FactoryGirl.create(:account)
  @request.host = "#{@account.subdomain}.example.com"
end