【问题标题】:Testing rails "association"测试rails“关联”
【发布时间】:2016-12-20 10:18:22
【问题描述】:

用户有很多个人资料。

测试父母没有问题。我一直在测试它的关联。

配置文件模型是:

class Profile < ApplicationRecord
  belongs_to :user

  validates_presence_of :first_name, :last_name
end

profile_test 文件是:

require 'test_helper'

class ProfileTest < ActiveSupport::TestCase

  test "create profile" do
    profile = Profile.new({"first_name"=>"test", "last_name"=>"test"})
    assert profile.save
  end
end

当我运行测试时,它得到一个失败:

Minitest::Assertion: Expected false to be truthy.

谁能告诉我为什么 Create 不起作用?

【问题讨论】:

    标签: ruby-on-rails testing minitest model-associations


    【解决方案1】:

    在Rails 5中,如果定义了belongs_to关联,则需要有关联记录。

    你的profile 没有设置user,所以profile.save 返回false。

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 2012-12-25
      • 1970-01-01
      相关资源
      最近更新 更多