本文介绍了RSpec: ActionView::Template::Error: 未定义方法 `full_name' 为 nil:NilClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开始规范测试时收到此错误.我了解未设置与用户的连接.我该怎么做才能通过测试?

RSpec 文件:posts_controller_spec.rb

需要'spec_helper'包括设计::TestHelpers描述 PostsController 做渲染视图上下文以用户身份登录"执行let(:user) { FactoryGirl.create(:user) }{ login_as(user) } 之前上下文在查看索引页面上"做let!(:p) { FactoryGirl.create(:post) }{ 获取:索引 } 之前它 { 应该 response_with(:success) }它 {assigns(:posts).should == [p] }结尾结尾结尾

我的观点:_home.html.erb

<% @posts.each do |post|%><%= link_to post.title, post %><%= post.user.full_name %></i></b><hr/><div><%= post.content.html_safe %>

<%结束%>

RSpec 失败:

PostsController 以用户身份登录查看索引页面失败/错误:{ get :index } 之前动作视图::模板::错误:nil:NilClass 的未定义方法full_name"# ./app/views/posts/_home.html.erb:5:in `block in _app_views_posts__home_html_erb__709569216_93469850'# ./app/views/posts/_home.html.erb:2:in `each'# ./app/views/posts/_home.html.erb:2:在`_app_views_posts__home_html_erb__709569216_93469850'# ./app/views/posts/index.html.erb:1:在`_app_views_posts_index_html_erb___825727830_90641210'# ./spec/controllers/posts_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
解决方案

没有看到您的 PostUser 模型,看来您需要更改

let!(:p) { FactoryGirl.create(:post) }

let!(:p) { FactoryGirl.create(:post, :user => user) }

let!(:p) { FactoryGirl.create(:post, :user_id => user.id) }

正确设置关联.

但是,如果您的视图假设 Post always 有一个 User,那么可能是您的 Post 模型应该验证 User 的存在,或者您应该更改视图以处理 Post 没有 User 的情况.

I am getting this error, when starting spec tests. I understand that connection with the user is not set. What shall I do to pass the tests?

RSpec file : posts_controller_spec.rb

require 'spec_helper'
include Devise::TestHelpers

describe PostsController do
  render_views

  context "Logged in as user" do
    let(:user) { FactoryGirl.create(:user) }

    before { login_as(user) }

    context "on viewing index page" do
      let!(:p) { FactoryGirl.create(:post) }

      before { get :index }

      it { should respond_with(:success) }

      it { assigns(:posts).should == [p] }
    end
  end
end

My view: _home.html.erb

<% @posts.each do |post| %>
  <%= link_to post.title, post %>

    <%= post.user.full_name %></i></b><hr />

    <div>
      <%= post.content.html_safe %>
    </div>
<% end %>

RSpec failures:

PostsController Logged in as user on viewing index page
     Failure/Error: before { get :index }
     ActionView::Template::Error:
       undefined method `full_name' for nil:NilClass
     # ./app/views/posts/_home.html.erb:5:in `block in _app_views_posts__home_html_erb__709569216_93469850'
     # ./app/views/posts/_home.html.erb:2:in `each'
     # ./app/views/posts/_home.html.erb:2:in `_app_views_posts__home_html_erb__709569216_93469850'
     # ./app/views/posts/index.html.erb:1:in `_app_views_posts_index_html_erb___825727830_90641210'
     # ./spec/controllers/posts_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
解决方案

Without seeing your model for Post and User, it looks like you need to change

let!(:p) { FactoryGirl.create(:post) }

to

let!(:p) { FactoryGirl.create(:post, :user => user) }

or

let!(:p) { FactoryGirl.create(:post, :user_id => user.id) }

to correctly set up the association.

However, if your view is assuming that a Post always has a User, maybe your Post model should validate the presence of a User, or maybe you should change your view to handle the case where a Post does not have a User.

这篇关于RSpec: ActionView::Template::Error: 未定义方法 `full_name' 为 nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:39