请帮助编写模型的测试。
模特用户:
class User < ActiveRecord::Base
has_many :guestbooks
has_secure_password
end
模特留言簿:
class Guestbook < ActiveRecord::Base
belongs_to :user
end
表用户:
id: integer
name: varchar
email: varchar
diary_name: varchar
表留言簿:
id: integer
message: text
user_id: integer
我正在尝试编写测试:
class GuestbookTest < ActiveSupport::TestCase
def setup
@user = User.create(
name: 'namee',
email: 'adddd@ad.ad',
diary_name: 'dnndndnррррnd',
password: 'qwerty',
password_confirmation: 'qwerty'
)
end
test "should save guestbook for signin user" do
guestbook = Guestbook.new(message: 'dfsdfsf gdfgfdghfghfghf ghfgh ', user_id: @user)
assert guestbook.save
end
end
收到以下错误消息:
kalinin@kalinin ~/rails/ZSUM $ rake test:models
Run options: --seed 19425
# Running:
F
Finished in 0.349347s, 2.8625 runs/s, 2.8625 assertions/s.
1) Failure:
GuestbookTest#test_should_save_guestbook_for_signin_user [/home/kalinin/rails/ZSUM/test/models/guestbook_test.rb:18]:
Failed assertion, no message given.
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
答案 0 :(得分:1)
溶液:
test "should save guestbook for signin user" do
guestbook = Guestbook.new(message: 'dfsdfsf gdfgfdghfghfghf ghfgh ', user_id: @user)
assert guestbook.save!
end
并在模型remakr名称验证中:
class Guestbook < ActiveRecord::Base
# validates :name, presence: true, length: { maximum: 200 }
validates :message, presence: true, length: { maximum: 1000, minimum: 10 }
belongs_to :user
end