我是测试单位的新手。我需要验证用户的电子邮件在用户表中是唯一的
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@usuario = User.new
@usuario.email="pepe@hotmail.com"
@usuario.password="123456"
end
test "user email must be unique" do
end
end
答案 0 :(得分:1)
好的,基于您的其他评论,我就是这样做的:
class UserTest < ActiveSupport::TestCase
setup do
@attrs = { email: "something@here.com", password: "123456" }
end
test "user email must be unique" do
assert User.create(@attrs)
assert user = User.new(@attrs)
assert user.invalid?
assert user.errors.include?(:email)
end
end