【问题标题】:rspec nil class error in controller spec控制器规范中的 rspec nil 类错误
【发布时间】:2016-05-24 21:30:25
【问题描述】:

我有一个带有avatar attr 和full_name 方法的配置文件模型,该方法使用first_namelast_name attrs。所有这些都在用户模型中委托。我真的不明白我怎么能得到 nil 类错误。如果我删除图像标签行测试通过,所以用户不能为零,因为我也调用user.full_name。我还有其他型号,我使用avatarfull_name 并且规格在那里工作正常。它也适用于开发环境。

我错过了什么?

它阻止的所有 3 个错误:

Failure/Error: <%= image_tag user.avatar.url(:small_thumb), class: "profile-index-avatar" %>

ActionView::Template::Error:
  undefined method `url' for nil:NilClass
  Did you mean?  URI

show.html.erb

<% @product.users.each_slice(2) do |user_row| %>
  <div class="row" style="padding-top:20px;">
    <% user_row.each do |user| %>
      <%= link_to user_profile_path(user) do %>
        <div class="col-md-6 product-user-column">
          <%= image_tag user.avatar.url(:small_thumb), class: "profile-index-avatar" %>
          <%= user.full_name %>
        </div>
      <% end %>
    <% end %>
  </div>
<% end %>

控制器规格

describe "GET show" do
  let!(:profile) { create(:profile, user: @user) }
  let!(:product) { create(:product, :product_with_nested_attrs) }
  let!(:product_user) { create(:product_user, user: @user, product: product, role: "owner") }
  before(:each) do
    get :show, id: product
  end

  it "assigns products" do
    expect(assigns(:product)).to eq(product)
    expect(assigns(:product).industry_products.size).to eq(1)
  end

  it { is_expected.to respond_with 200 }
  it { is_expected.to render_template :show }
end

更新

根据 zetetic 的建议,我检查了头像是否为零。 index 操作的用户头像不是 nil,show 操作的头像也不是。

如果在显示页面包含图像标签时将“avatar not nil for show”方法放入,那么它会引发与其余部分相同的错误。如果我删除图像标签行,那么它会说它不是零。

describe "GET index" do
  let!(:profile) { create(:profile, user: @user) }
  let!(:product) { create(:product, :product_with_nested_attrs) }
  before(:each) do
    get :index
  end

  it "avatar not nil for show" do
    expect(@user.avatar).to_not be_nil
  end

  it "assigns products" do
    expect(assigns(:products)).to eq([product])
  end

  it { is_expected.to respond_with 200 }
  it { is_expected.to render_template :index }
end

describe "GET show" do
  let!(:profile) { create(:profile, user: @user) }
  let!(:product_user) { create(:product_user, user: @user, product: product, role: "owner") }
  let!(:product) { create(:product, :product_with_nested_attrs) }
  before(:each) do
    get :show, id: product
  end

  it "avatar not nil for show" do
    expect(@user.avatar).to_not be_nil
  end

  it "assigns products" do
    expect(assigns(:product)).to eq(product)
    expect(assigns(:product).industry_products.size).to eq(1)
  end

  it { is_expected.to respond_with 200 }
  it { is_expected.to render_template :show }
end

【问题讨论】:

  • @user 有值吗?如果用户丢失,工厂会报错吗?
  • zetetic, @user 用于所有操作(索引、销毁等)。所以它工作正常(28 个示例和 3 个失败)。正如我所写的,如果我取出带有图像标签的行,它会以user.full_name 传递。在我决定放入图像标签之前,所有规范都已通过。
  • 我会仔细查看工厂和/或模型,以确保它们返回您期望的值。临时添加额外的期望值通常有助于确保值确实存在(例如expect(@user.avatar).to_not be_nil
  • zetetic,我会在 3 分钟内更新我的问题,请看一下。我发现了一些奇怪的东西。

标签: ruby-on-rails ruby rspec factory-bot rspec-rails


【解决方案1】:

其中一位用户的头像为零

【讨论】:

  • JaTo,我猜只在测试环境中。如果是这样,我该如何解决这个问题?
  • 检查您的用户工厂是否创建了头像。还要检查头像是否响应 url
  • JaTo,工厂似乎工作正常。请查看我的更新。如果我从视图中取出图像标签,那么它会说头像不是 nil。
猜你喜欢
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多