我配置了Rspec Ruby 2.0.0-p0和Rails 3.2.14配置是完美的我確信但是當我嘗試運行耙規格:控制器,它gaves我下面的錯誤寫在規範例如每請求動作 -*** NoMethodError異常:未定義的方法`to_sym'爲零:Rails 3.2中的NilClass與控制器中的Rspec規範
*** NoMethodError Exception: undefined method `to_sym' for nil:NilClass
我寫的規格爲前控制器,但從來沒有碰到過這樣的情況,幫助我,如果任何一個已修復相同的問題....
這是我的規格和錯誤堆棧
describe UsersController do
before (:each) do
@user = FactoryGirl.create(:user)
sign_in @user
end
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
end
describe "GET 'show'" do
it "should be successful" do
get :show, :id => @user.id
response.should be_success
end
it "should find the right user" do
get :show, :id => @user.id
assigns(:user).should == @user
end
end
end
下面是結果 -
Failures:
1) UsersController GET 'index' should be successful
Failure/Error: get 'index'
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
2) UsersController GET 'show' should be successful
Failure/Error: get :show, :id => @user.id
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:21:in `block (3 levels) in <top (required)>'
3) UsersController GET 'show' should find the right user
Failure/Error: get :show, :id => @user.id
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:26:in `block (3 levels) in <top (required)>'
Finished in 0.4741 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:12 # UsersController GET 'index' should be successful
rspec ./spec/controllers/users_controller_spec.rb:20 # UsersController GET 'show' should be successful
rspec ./spec/controllers/users_controller_spec.rb:25 # UsersController GET 'show' should find the right user
Randomized with seed 19701
我廠是 -
FactoryGirl.define do
factory :user do
first_name 'Test User'
last_name 'Last name'
email '[email protected]'
password 'changeme'
password_confirmation 'changeme'
company 'RR'
confirmed_at Time.now
end
end
我們需要的公司名稱必須建立登記用戶。
class UsersController < ApplicationController
before_filter :authenticate_user!
#authorize_resource
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
end
這裏我已經添加了兩種測試用戶控制器的方法。
我與偉大工程,但不是在我的項目演示的例子嘗試同樣的事情.... 感謝
請分享您的規格,代碼和完整的堆棧跟蹤。 –
嗨Graeme請檢查更新的問題的更多細節。謝謝 –
請分享您的用戶工廠和型號。 –