spec / controllers / profiles_controller_spec.rb中有一个奇怪的错误:未定义的局部变量或方法'profile'
粘贴在控制器的相关部分:
require 'rails_helper'
require 'factory_girl'
describe ProfilesController, :type => :controller do
login_user #this is defined elsewhere, not an issue
describe "PUT update" do
before(:each) do
@profile = FactoryGirl.create(:profile)
end
context "valid attributes" do
it "located the requested @profile" do
put :update, id: @profile, profile: FactoryGirl.attributes_for(:profile)
assigns(:profile).should eq(@profile)
end
end
end
end
答案 0 :(得分:1)
我的猜测是您的规格或工厂没有问题。问题在于您正在测试的控制器。
您看到的例外
1) ProfilesController PUT #update valid attributes located the requested @profile
Failure/Error: put :update, id: @profile, profile: FactoryGirl.attributes_for(:profile)
NameError:
undefined local variable or method `profile' for #<ProfilesController:0x007fae343cfe08>
告诉您ProfilesController
有未定义的局部变量或方法profile
。
在你的控制器中,你有这一行
if @profile.update(profile)
最有可能导致错误。也许您需要将update(profile)
更改为update(profile_params)
?如果没有看到整个控制器,这很难说,但这将遵循一个共同的模式。我想你有一个方法profile_params
可以返回过滤后的参数。
答案 1 :(得分:0)
您是否在test/factories.rb
或spec/factories.rb
?
它看起来像这样:
# This will guess the User class
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Doe"
admin false
end
# This will use the User class (Admin would have been guessed)
factory :admin, class: User do
first_name "Admin"
last_name "User"
admin true
end
end
直接取自docs