我正在尝试进入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)>'
这是特定的测试:
require 'rails_helper'
include Devise::TestHelpers
describe UsersController do
before (:each) do
@user = FactoryGirl.create(:user)
sign_in @user
end
describe "GET 'show'" do
it "should find the right user" do
get :show, :id => @user.id
puts "user = #{@user.inspect}"
user = assigns(:user)
#assigns(:user).should == @user
puts "assigns user = #{assigns(:user)}"
expect(user).to eq(@user)
end
end
end
这是控制器:
class UsersController < ApplicationController
...
def show
authorize! :show, @user, :message => 'Not authorized as an administrator.'
@user = current_account.users.find(params[:id])
end
...
end
application_controller.rb:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
check_authorization :unless => :devise_controller?
before_filter :authenticate_user!, :validate_subdomain
helper_method :subdomain, :current_account
layout :set_layout
# This will redirect the user to your 404 page if the account can not be found
# based on the subdomain. You can change this to whatever best fits your
# application.
def validate_subdomain
current_account
end
def current_account
@current_account ||= Account.where(:subdomain => subdomain).first
#puts @current_account
if @current_account.nil?
redirect_to('/accounts/invalid_site')
return
end
@current_account
end
def subdomain
request.subdomain
end
...
end
用户工厂:
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
如何解决此错误消息?我觉得它与before_filters有关,但这只是猜测。
答案 0 :(得分:0)
您的用户控制器上是否有需要身份验证才能使用show方法的before_filter?您的前一个块正在进行两次调用,创建一个用户然后&#34; sign_in&#34;,某种身份验证。但您的测试只是查看用户是否已创建,并且show方法返回带有传递的id的正确用户记录。
所以我会打破你的前一块。首先测试您的工厂是否成功创建:
it 'has a valid factory' do
FactoryGirl.create(:user).should eq(true) # or should be_valid..
end
如果通过,我会在用户控制器上(临时)注释掉sign_in方法的任何约束,并测试show是否收到id并返回正确的用户。
如果通过,则重新添加约束以在用户控制器上进行验证,并找出如何测试sign_in方法。 :)
答案 1 :(得分:0)
好的,所以我通过current_account没有设置来调试它。我找到了这个答案Rails rspec set subdomain。
我想我在原始问题中遗漏了一大块难题,所以我为此道歉,因为我不知道这会影响我的rspec。
诀窍是
before(:each) do
@account = FactoryGirl.create(:account)
@request.host = "#{@account.subdomain}.example.com"
end