1

I get this errormsg from doing tests in Rails 4

Error:
ArticlesControllerTest#test_x:
ActionView::Template::Error: undefined method `state' for nil:NilClass

The error occurs at this line in the view:

<% @articles.each do |article| %>
    <div id="article-container">
      <div id="article-content">
        <ul>
          <li> <b>Status:</b> <%= article.status_id %></li>
          <li> <b>State:</b> <%= article.state.state %></li

>

The model looks like this for article

  belongs_to :user
  belongs_to :book, primary_key: :ISBN, foreign_key: :ISBN
  has_one :state, primary_key: :states_id, foreign_key: "id"
  has_one :status, primary_key: :status_id, foreign_key: "id"

  has_and_belongs_to_many :courses

  validates :ISBN, presence: true
  validates :states_id, presence: true
  validates :user_id, presence: true
  validates :price, presence: true
  validates :status_id, presence: true

This is the article and states table in schema

Article table:

  create_table "articles", force: :cascade do |t|
    t.string   "ISBN"
    t.integer  "user_id"
    t.integer  "price"
    t.integer  "status_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "states_id"
    t.index ["states_id"], name: "index_articles_on_states_id"
  end

State table:

  create_table "states", force: :cascade do |t|
    t.string   "state"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

The problem is running this test (showing file from top until the test in question):

require 'test_helper'

class ArticlesControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
  setup do
    @article_normal = articles(:normal_article)
    @article_admin = articles(:admin_article)
    @normal_user = users(:normal_user)
    @admin_user = users(:admin_user)

    @normal_user.id = @article_normal.user_id
    @admin_user.id = @article_admin.user_id
  end
  test "test x" do
    get articles_url
    assert_response :success
  end

The test provides the error message, while in localhost it works.

EDIT The test database is populated with fixtures.

Article fixtures

normal_article:
  ISBN: 9788252179675
  user_id: 1
  price: 500
  states_id: 1
  status_id: 1


admin_article:
  ISBN: 9788252179675
  user_id: 2
  price: 400
  states_id: 1
  status_id: 1

The controller method from article_controller:

# GET /articles
  # GET /articles.json
  def index
    @articles = Article.all
    @courses = Course.all
    @states = State.all
    if params[:search]
      @articles = Article.search(params[:search])
    else
      @articles = Article.all
    end
  end
4

1 回答 1

1

article的灯具没有state正确引用灯具。

state而不是通过固定引用 a id(您state_id: 1在您的夹具中使用:),为state您的状态夹具中的每个指定一个正确的名称并使用该名称,如下所示:state: valid_state


Rails 指南中引用关于如何使用固定装置中的关联:

如果您正在使用关联,您可以简单地在两个不同的设备之间定义一个参考节点。这是一个带有belongs_to/has_many关联的示例:

# In fixtures/categories.yml
about:
 name: About

# In fixtures/articles.yml
first:
  title: Welcome to Rails!
  body: Hello world!
  category: about

请注意,在 中找到categoryfirst文章fixtures/articles.yml的键值为about。这告诉 Rails 加载关于在fixtures/categories.yml.

对于通过名称相互引用的关联,您可以使用夹具名称而不是指定关联夹具上的 id: 属性。Rails 将自动分配一个主键以在运行之间保持一致。[...]

于 2017-03-17T16:56:00.343 回答