尽管应用程序中有功能,但创建项目测试失败

时间:2013-05-06 15:30:00

标签: ruby-on-rails rspec

尽管知道应用程序中的功能有效,但我还是测试失败了。我的直觉说我应该尝试保存我创建的东西,但我不确定如何在assert_difference块中执行此操作,因为它看起来不像新的thing被分配给我可以使用的变量.save。感谢您提供的任何建议。

测试:

test "should create thing" do
    assert_difference('thing.count') do
      post :create, thing: { thing_type_id: @thing.thing_type_id, name: @thing.name}
    end

输出:

 1) Failure:
test_should_create_thing(thingsControllerTest) [C:/../thing_controller_test.rb:20]:
"thing.count" didn't change by 1.
<3> expected but was
<2>.

1 个答案:

答案 0 :(得分:2)

听起来你的数据库中可能有一些遗留状态。我看到expected but was <2>,这意味着您的数据库中已经有两个Thing

您可以尝试在测试之间清除DB状态。根据您的数据库,查看database_cleaner gem。

此外,您似乎已经通过@thing的存在创建了该对象。如果是这种情况,这可以按预期工作。

您可以通过测试正常的Thing::create

将控制器从等式中取出来验证这一点
test "creates a new Thing" do
  assert_difference('Thing.count') do
    Thing.create thing_type_id: @thing.thing_type_id, name: @thing.name
  end
end