Rails MiniTest,为什么这个过程不是成功而不是错误?


[woo:~/Development … e-roles/test] master(+7/-3) 23s ± rails test
Running via Spring preloader in process 18905
Run options: --seed 34925
# Running:
E
Error:
UserTest#test_User_must_have_an_email_address:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2019-05-16 23:08:53.696224', '2019-05-16 23:08:53.696224', 298486374)

bin/rails test test/models/user_test.rb:4

Finished in 0.019202s, 52.0779 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

现在,这是user_test.rb

[woo:~/Development … e-roles/test] master(+7/-3) 1 ± cat models/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
   test "User must have an email address" do
     user = User.new
     assert_raise SQLite3::ConstraintException   do
     user.save
     end
    end
end

我认为这将是成功的过程,因为我断言这是为了提出sqlite3 :: benteraintexception。如果提出了适当的例外,我想让测试成功。我在做什么错?

我认为要通过此测试,您需要断言Ruby异常,而不是SQLite3异常。您的activerecord模型(我猜(是定义约束的地方。

类似的东西:

assert_raise ActiveRecord::RecordNotUnique do
  user.save
end

关于Ruby Exception的断言,即使您将来切换到其他数据库,这是使用ORM的主要原因之一。

相关内容

  • 没有找到相关文章

最新更新