2011-06-12 25 views
19

我很努力在Rails中测试更新方法。我正在使用标准内置测试框架(test::unit)和Rails 3.0.8。我已经创建了一个用于测试的最小应用程序,但是我无法使其工作。这是我做的:如何在Rails中测试更新方法

我创建一个新的空白Rails应用程序:

rails new testapp 

创建一个模式叫收藏:

rails generate model collection name:string 

运行耙分贝:迁移:

rake db:migrate 

使用更新方法创建一个名为Collections的控制器:

rails generate controller collections update 

在collection_controller.rb我添加此最小更新方法:

def update 
    @collection = Collection.find(params[:id]) 
    @collection.update_attributes(params[:collection]) 
end 

的测试夹具是默认值(collections.yml):

one: 
    name: MyString 

two: 
    name: MyString 

然后,添加这个到collection_controller_test。 rb下功能:

test "should update collection" do 
    put :update, :id => collections(:one), :collection => {:name => 'MyString2'} 
    assert_equal "MyString2", collections(:one).name 
end 

当我运行测试:

rake test:functionals 

它失败,此消息:

test_should_update_collection(CollectionsControllerTest) [/Users/atle/Documents/Rails/testapp/test/functional/collections_controller_test.rb:6]: 
<"MyString2"> expected but was 
<"MyString">. 

下面是test.log中输出:

[1m[36mSQL (0.2ms)[0m [1m SELECT name 
FROM sqlite_master 
WHERE type = 'table' AND NOT name = 'sqlite_sequence' 
[0m 
[1m[35mCollection Load (0.1ms)[0m SELECT "collections".* FROM "collections" WHERE "collections"."id" = 980190962 LIMIT 1 
Processing by CollectionsController#update as HTML 
Parameters: {"id"=>#<Collection id: 980190962, name: "MyString", created_at: "2011-06-12 13:14:13", updated_at: "2011-06-12 13:14:13">, "collection"=>{"name"=>"MyString2"}} 
[1m[36mCollection Load (0.2ms)[0m [1mSELECT "collections".* FROM "collections" WHERE "collections"."id" = 980190962 LIMIT 1[0m 
[1m[35mAREL (0.2ms)[0m UPDATE "collections" SET "name" = 'MyString2', "updated_at" = '2011-06-12 13:14:13.394135' WHERE "collections"."id" = 980190962 
Rendered collections/update.html.erb within layouts/application (1.5ms) 
Completed 200 OK in 9ms (Views: 4.5ms | ActiveRecord: 1.2ms) 

在日志中我可以看到UPDATE声明,但我从来没有看到一个新的SELECT声明。

有人可以向我解释我做错了吗?

+2

对于一个明确提出的问题+1。很高兴看到一位新用户询问这个详细程度的明确问题。你的问题肯定来自你的断言中的'collections(:one)',它只是从夹具中读取数据,而不是数据库。 Cameron和Teddy在下面都有这个正确答案。 (顺便说一句:看看'factory_girl'和转储fixture完全尽快。他们很方便入门,但后来一个可怕的混乱。) – jdl 2011-06-12 14:20:19

+0

所以夹具和数据库是两个不同的位置。我假设在启动时将fixtures读入数据库,并且从db读取集合(:one)。谢谢你清理的东西,我会看看factory_girl :) – Atle 2011-06-12 15:18:19

回答

13

您正在重新加载夹具数据,而不是从数据库中读取数据。尝试类似

assert_equal "MyString2", Collection.find(collections(:one)).name 

您可能需要在灯具中指定ID。

声明:我还没有测试过这个。

+1

这工作,现在我的测试工作,谢谢! – Atle 2011-06-12 15:05:23

+0

如果您不需要,请不要在测试中使用数据库。你不想测试数据库是否工作,对吗? – schmijos 2015-07-16 12:29:52

17

您可以对HTTP响应期间创建的实例变量声明:

test "should update collection" do 
    put :update, :id => collections(:one), :collection => {:name => 'MyString2'} 
    assert_equal "MyString2", assigns(:collection).name 
end 
+0

您的建议也奏效,但自从他第一次以来,我接受了卡梅隆的答案;)谢谢! – Atle 2011-06-12 15:07:23

+0

这是更好的答案,因为它不使用数据库。 – schmijos 2015-07-16 12:30:28

+0

这是正确的答案。 Collection.find(集合(:一))是丑陋的。 – hellion 2016-05-08 19:14:26

2

我的这个喜爱的变种:

test "should update collection" do 
    coll = collections(:one) 
    put :update, :id => coll, :collection => {:name => "MyString2"} 
    assert_equal "MyString2", coll.reload.name 
end 

assigns(:collection)招实际上并不保证记录被保存了。这可能不是问题,也可能是一个惊喜。

+0

如果可能,请使用'assigns'。你不需要为你想测试的任何数据库交互。 – schmijos 2015-07-16 12:31:16

相关问题