2016-12-20 11 views
0

ユーザーには多くのプロファイルがあります。テストレール "関連"

親のテストに問題はありません。私はその関連性をテストすることに固執しています。

プロファイルモデルは次のとおりです。

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がうまくいかない理由を明確にしてください。

答えて

0

Rails 5では、belongs_toの関連付けが定義されている場合、関連付けレコードが必要です。

profileuserを設定しないため、profile.saveはfalseを返します。