Ruby on rails 奇怪的rspec错误`未定义的方法模型\u名称`
我到处搜索,发现了这个问题和其他问题,但我的应用程序完全可以工作,只是我的测试抛出了错误 _form.html.hamlRuby on rails 奇怪的rspec错误`未定义的方法模型\u名称`,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我到处搜索,发现了这个问题和其他问题,但我的应用程序完全可以工作,只是我的测试抛出了错误 _form.html.haml = simple_form_for @employer_profile do |f| =f.error_notification .form-inputs =f.input :company_name ... .form-actions =f.button :submit, 'Create Employer profile', id: 'employer-pr
= simple_form_for @employer_profile do |f|
=f.error_notification
.form-inputs
=f.input :company_name
...
.form-actions
=f.button :submit, 'Create Employer profile', id: 'employer-profile-submit'
我甚至不知道还要粘贴什么代码,我没有在应用程序的任何地方定义model\u name
,在任何视图或我检查内容时的测试中都没有任何内容是零
完全错误
1) Creating Employer Profiles Employer Profile edits a profile
Failure/Error: visit employer_profiles_edit_path(@user)
ActionView::Template::Error:
undefined method `model_name' for NilClass:Class
# ./app/views/employer_profiles/_form.html.haml:1:in `_app_views_employer_profiles__form_html_haml__754845775744985234_44321300'
# ./app/views/employer_profiles/edit.html.haml:4:in `_app_views_employer_profiles_edit_html_haml__3107954110108075276_44064480'
# ./spec/features/employer_profiles_spec.rb:44:in `block (3 levels) in <top (required)>'
耙道
employer_profiles_new GET /employer_profiles/new(.:format) employer_profiles#new
employer_profiles POST /employer_profiles(.:format) employer_profiles#create
employer_profile GET /employer_profile/:user_id(.:format) employer_profiles#show
employer_profiles_edit GET /employer_profiles/:user_id/edit(.:format) employer_profiles#edit
PUT /employer_profiles/:user_id(.:format) employer_profiles#update
@employer\u profile
在您的视图中为零-因此在您的编辑操作中employer profile.find\u by\u user\u id(params[:user\u id])
返回零
如果您希望它在找不到EmployerProfile时引发异常,请使用该方法的变体(即,find\u by\u user\u id!
)
我怀疑您的编辑路由提供了
params[:id]
,您正在查找params[:user\u id]
。检查rake routes的输出
或检查控制器中的参数。简单的表单
是rails的魔法。您的@employer\u档案
似乎未定义。你的行为是什么样子的?更新了full controller,因为我甚至不知道。你也可以发布失败的测试吗?更新了功能testadded rake routes,我认为没有任何错误。我认为这只是一个奇怪的简单形式的事情,也许我没有完全遵循惯例,所以它给我带来了一些奇怪?不管怎样,我只是使用了默认的表单,并且已经克服了这个错误。它不是一个“奇怪的简单表单”@employer\u profile
为零。我将编辑操作从employeerprofile.find\u by\u user\u id(params[:user\u id])
更改为使用@user.employer\u profile
设置@employer\u profile
。你知道为什么后者有效,而前者无效吗?如果参数[:user\u id]
与@user.id
不同,就不能保证会发生什么。没有足够的信息可以说
require 'spec_helper'
include Warden::Test::Helpers
Warden.test_mode!
def login_user
before(:each) do
@user = FactoryGirl.create(:user)
login_as(@user)
end
end
feature "Creating Employer Profiles" do
login_user
describe 'Employer Profile' do
describe 'create profile' do
it 'completes a profile and shows the results' do
visit '/'
expect{
click_link('New Employer Profile')
fill_in 'Company name', with: 'Lost Tie LLC'
fill_in 'Employer rating', with: 1
fill_in 'Description', with: 'testing text'
fill_in 'City', with: 'test-city'
fill_in 'State', with: 'test-state'
fill_in 'Email', with: 'test@example.com'
fill_in 'Website', with: 'www.example.com'
fill_in 'Address', with: '123 example street'
fill_in 'Zip', with: 12345
fill_in 'Industry', with: 'Oil'
click_button 'Create Employer profile'
}.to change(EmployerProfile,:count).by(1)
page.should have_content 'Lost Tie LLC'
end
end
it "edits a profile" do
visit employer_profiles_edit_path(@user)
expect(EmployerProfile.find_by_user_id(params[:user_id])).to_not be_nil
end
employer_profiles_new GET /employer_profiles/new(.:format) employer_profiles#new
employer_profiles POST /employer_profiles(.:format) employer_profiles#create
employer_profile GET /employer_profile/:user_id(.:format) employer_profiles#show
employer_profiles_edit GET /employer_profiles/:user_id/edit(.:format) employer_profiles#edit
PUT /employer_profiles/:user_id(.:format) employer_profiles#update