lundi 29 janvier 2018

Rails Minitest ActionView::Template::Error: undefined method for nil:NilClas

Been trying to debug this for two days and I'm just stuck. I have a view that I'm trying to test in rails and the view works perfectly when I test manually in the browser but I keep getting this error in my controller test:

ActionView::Template::Error: undefined method "name" for nil:NilClass Here is the full error message.

Here's the test (test/controllers/quotes_controller_test.rb):

  test "should get quotes index as browse" do
    get browse_path
    assert_response :success
  end

It's breaking where I render this partial (views/quotes/browse.html.erb):

  <% if @recent_quotes.any? %>
    <aside class="recent-quotes browse-quotes col-7">
      <%= render @recent_quotes %>
    </aside>
  <% end %>

The partial looks like this (views/quotes/_quote.html.erb):

<blockquote id="quote<%= quote.id %> blockquote">
  <small class="text-muted">
    <%= link_to quote.topic.name, artist_path(quote.topic) %>
  </small>
  <p class="mb-0"><%= link_to quote.content, quote_path(quote) %></p>

  <footer class="blockquote-footer">
      <cite title="Source">
        <%= link_to quote.speaker.name, quote.source %>
      </cite>
  </footer>
</blockquote>

And the controller action looks like this (controllers/quotes_controller.rb):

  def browse
    @artists = Artist.all
    @recent_quotes  = Quote.all.limit(7)
  end

Again, everything works great in the browser, but I can't get that simple test to pass. If I remove the partial it passes, so the route is working fine. I think that the test is just looking for the name method in the first call to quote.topic.name and not finding it.

But it's working in the browser, so I can't figure out what I'm doing wrong with this test.

Aucun commentaire:

Enregistrer un commentaire