小编Atl*_*tle的帖子

如何在Rails中测试更新方法

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

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

rails new testapp
Run Code Online (Sandbox Code Playgroud)

创建一个名为Collection的模型:

rails generate model collection name:string
Run Code Online (Sandbox Code Playgroud)

运行rake db:migrate:

rake db:migrate
Run Code Online (Sandbox Code Playgroud)

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

rails generate controller collections update
Run Code Online (Sandbox Code Playgroud)

在collection_controller.rb中,我添加了这个最小更新方法:

def update
  @collection = Collection.find(params[:id])
  @collection.update_attributes(params[:collection])
end
Run Code Online (Sandbox Code Playgroud)

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

one:
  name: MyString

two:
  name: MyString
Run Code Online (Sandbox Code Playgroud)

然后我将它添加到功能下的collection_controller_test.rb中:

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

当我运行测试时:

rake test:functionals
Run Code Online (Sandbox Code Playgroud)

它失败了这条消息:

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

以下是test.log的输出:

[1m[36mSQL (0.2ms)[0m  [1m SELECT name
FROM sqlite_master
WHERE type …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails functional-testing

23
推荐指数
2
解决办法
8675
查看次数

标签 统计

functional-testing ×1

ruby-on-rails ×1