Rails 4 - 所有测试失败(甚至断言为真)

时间:2015-10-27 09:57:38

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

对rails很新,刚刚开始学习测试。我们遇到了一个问题,可能有一个简单的答案让我们难过。

我们编写了一些代码并使其在开发中工作(只是静态代码和具有Devise的登录系统)。我们文件中的唯一测试用例是由rails生成的,例如:

  test "should get about" do
    get :about
    assert_response :success
  end

然而,当我们运行bundle exec rake test时,令我们惊讶的是,我们每次测试都失败

10 runs, 0 assertions, 0 failures, 10 errors, 0 skips

每个测试都有相同的错误:

ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "email" violates not-null constraint
DETAIL:  Failing row contains (980190962, null, 2015-10-27 09:40:30, 2015-10-27 09:40:30).
: INSERT INTO "leads" ("created_at", "updated_at", "id") VALUES ('2015-10-27 09:40:30', '2015-10-27 09:40:30', 980190962)

通过阅读该错误,似乎我们的leads表中没有电子邮件的记录。电子邮件是迁移后稍后创建的字段。但是,通过psqlAdmin查看,每行都有一封电子邮件。此外,如果我们在使用chrome加载页面后查看控制台,则没有错误。

即使是最基本的测试也会失败并出现同样的错误:

test "the truth" do
    assert true
end

我唯一的猜测是有一些默认播种正在进行,并且它试图在没有电子邮件的情况下将字段插入到潜在客户中。但是,seeds.rb完全空白。

以下是终端呼叫的开始:

Macintosh-2:triplanding Calvino$ bundle exec rake test
Run options: --seed 15348

# Running:

EEEEEEEEEE

Finished in 0.108843s, 91.8753 runs/s, 0.0000 assertions/s.
  1. 为什么这些测试失败?
  2. 为什么leads甚至参与了像assert true这样的测试。
  3. 提前谢谢!

    更新

    这是我的fixture / leads.yml文件。我们没有编辑它,这是生成的:

    one: {}
    # column: value
    #
    two: {}
    #  column: value
    

1 个答案:

答案 0 :(得分:3)

Moste可能是因为你的灯具因以下原因无法运行:

ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "email" violates not-null constraint

检查你的导线夹具是否包含空电子邮件,它应该可以正常工作!

每当你在rails上运行测试时,它都会设置测试数据库(这就是assert true失败的原因)