验证用户电子邮件在测试单元导轨中是否唯一

时间:2015-05-06 01:41:35

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 testing

我是测试单位的新手。我需要验证用户的电子邮件在用户表中是唯一的

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

1 个答案:

答案 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