ControllerTest 错误,找不到 'id'= 的交易
ControllerTest error, Couldn't find Transaction with 'id'=
我的控制器的 Show 动作没有通过单元测试。我在 ControllerTest 中遇到错误。这是我从 'rake test':
得到的错误
> ERROR["test_should_get_show", TransactionsControllerTest, 2015-07-18
> 00:30:18 -0400] test_should_get_show#TransactionsControllerTest
> (1437193818.29s) ActiveRecord::RecordNotFound:
> ActiveRecord::RecordNotFound: Couldn't find Transaction with 'id'=
> app/controllers/transactions_controller.rb:21:in `show'
> test/controllers/transactions_controller_test.rb:6:in `block in <class:TransactionsControllerTest>'
> app/controllers/transactions_controller.rb:21:in `show'
> test/controllers/transactions_controller_test.rb:6:in `block in <class:TransactionsControllerTest>'
>
> 5/5:
> [=======================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00
>
> Finished in 0.24993s 5 tests, 4 assertions, 0 failures, 1 errors, 0
> skips
尽管有这个错误,当我使用这个 url 时,视图还是成功地在浏览器中生成了页面:
http://localhost:3000/transactions/1
所以,某处出错了。它没有失败。
transactions_controller.rb
class TransactionsController < ApplicationController
def new
@transaction = Transaction.new
end
def create
@transaction = Transaction.new(transaction_params)
if @transaction.save
# Handle a successful save.
else
render 'new'
end
end
def index
@transactions = Transaction.all
end
def show
@transaction = Transaction.find(params[:id]) #this is row 21
end
private
def transaction_params
params.require(:transaction).permit(:company, :month, :account_code, :description, :transaction_amount)
end
end
transaction_controller_test.rb
require 'test_helper'
class TransactionsControllerTest < ActionController::TestCase
test "should get show" do
transaction = Transaction.create
get :show, id: transaction.id #this is row 6
assert_response :success
end
end
以下夹具由 rails 自动生成。我是 rails 的新人,不得不承认我并不真正了解固定装置。
transactions.yml
one:
company: MyString
month: 2015-06-19
account_code: MyString
description: MyString
transaction_amount: 1.5
two:
company: MyString
month: 2015-06-19
account_code: MyString
description: MyString
transaction_amount: 1.5
在我的路由文件中,我将根设置为索引操作。我计划通过 url http://localhost:3000/transactions/1 访问 show 操作并且(如上所述)它确实有效。
routes.rb
Rails.application.routes.draw do
root 'transactions#index'
resources :transactions
end
transaction.rb
class Transaction < ActiveRecord::Base
validates :month, presence: true
validates :account_code, presence: true
end
transaction_test.rb
require 'test_helper'
class TransactionTest < ActiveSupport::TestCase
def setup
#@transaction = transactions(:one)
@transaction = Transaction.new(company: "Inc", month: Date.new(2015,6,15).to_s,
account_code: "80-32100-12-1201-60010",
description: "Salaries & Wages - Histo Gross 1", transaction_amount: 100000)
end
test "should be valid" do
assert @transaction.valid?
end
test "month should be present" do
@transaction.month = " "
assert_not @transaction.valid?
end
test "account_code should be present" do
@transaction.account_code = " "
assert_not @transaction.valid?
end
test "month cannot be a string" do #not sure if this is worth anything at all
@transaction.month = "xxxxxxxxxxxx"
assert_not @transaction.valid?
end
end
Rails生成的test auto(在我修改之前)很简单:
require 'test_helper'
class TransactionsControllerTest < ActionController::TestCase
test "should get show" do
get :show
assert_response :success
end
end
我将其修改为上面的当前版本,因为(我相信)测试应该首先创建一个事务,然后以某种方式引用 id。但我不确定我这样做是否正确。
我在 Whosebug 上看到过类似的问题。例如,这个问题很有帮助,但并没有完全把我带到那里
Count; find User with id= Minitest
注意:最初,我错误地设置了控制器。我把它设为单数"TransactionController",改成了复数"TransactionsController"。所以我也不得不在单元测试中更改名称。也许这对应用程序有影响。
所以...问题:
- 为什么单元测试失败而显示操作实际上是
在浏览器中工作?
- 我如何更改单元测试中的代码以便它识别
我正在使用 transaction_params?
- 当我修改灯具时,是否必须修改灯具?
unit_test 还是控制器?
好的,coorasse 的建议是使用 create!强制应用发现验证错误。初始错误(Couldn't find Transaction with 'id'= )已替换为新错误:
ActiveRecord::RecordInvalid: Validation failed: Month can't be blank,
Account code can't be blank
这让我感到困惑,因为它让我想到了模型验证错误。为什么我在 ControllerTest 中收到模型验证错误?此外,transaction.rb 模型清楚地表明我已添加验证以满足 Month 和 AccountCode 的存在。
所以,我花了一些时间通读 "A Guide to Testing Rails Applications" Rails 指南
http://guides.rubyonrails.org/testing.html
在第 8 节中,它展示了如何在每个测试之前包含一个代码块。这是新的 transaction_controller_test
transaction_controller_test.rb(已修订)
require 'test_helper'
class TransactionsControllerTest < ActionController::TestCase
# called before every single test
def setup
@transaction = transactions(:one)
end
test "should get show" do
get :show, id: @transaction.id
assert_response :success
end
end
此后测试通过!不幸的是,Rails指南没有告诉我为什么我想在控制器测试中使用设置。如果任何聪明的 Rails 专家对此有一些指导,您的意见将不胜感激。
我的控制器的 Show 动作没有通过单元测试。我在 ControllerTest 中遇到错误。这是我从 'rake test':
得到的错误> ERROR["test_should_get_show", TransactionsControllerTest, 2015-07-18
> 00:30:18 -0400] test_should_get_show#TransactionsControllerTest
> (1437193818.29s) ActiveRecord::RecordNotFound:
> ActiveRecord::RecordNotFound: Couldn't find Transaction with 'id'=
> app/controllers/transactions_controller.rb:21:in `show'
> test/controllers/transactions_controller_test.rb:6:in `block in <class:TransactionsControllerTest>'
> app/controllers/transactions_controller.rb:21:in `show'
> test/controllers/transactions_controller_test.rb:6:in `block in <class:TransactionsControllerTest>'
>
> 5/5:
> [=======================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00
>
> Finished in 0.24993s 5 tests, 4 assertions, 0 failures, 1 errors, 0
> skips
尽管有这个错误,当我使用这个 url 时,视图还是成功地在浏览器中生成了页面: http://localhost:3000/transactions/1
所以,某处出错了。它没有失败。
transactions_controller.rb
class TransactionsController < ApplicationController
def new
@transaction = Transaction.new
end
def create
@transaction = Transaction.new(transaction_params)
if @transaction.save
# Handle a successful save.
else
render 'new'
end
end
def index
@transactions = Transaction.all
end
def show
@transaction = Transaction.find(params[:id]) #this is row 21
end
private
def transaction_params
params.require(:transaction).permit(:company, :month, :account_code, :description, :transaction_amount)
end
end
transaction_controller_test.rb
require 'test_helper'
class TransactionsControllerTest < ActionController::TestCase
test "should get show" do
transaction = Transaction.create
get :show, id: transaction.id #this is row 6
assert_response :success
end
end
以下夹具由 rails 自动生成。我是 rails 的新人,不得不承认我并不真正了解固定装置。
transactions.yml
one:
company: MyString
month: 2015-06-19
account_code: MyString
description: MyString
transaction_amount: 1.5
two:
company: MyString
month: 2015-06-19
account_code: MyString
description: MyString
transaction_amount: 1.5
在我的路由文件中,我将根设置为索引操作。我计划通过 url http://localhost:3000/transactions/1 访问 show 操作并且(如上所述)它确实有效。
routes.rb
Rails.application.routes.draw do
root 'transactions#index'
resources :transactions
end
transaction.rb
class Transaction < ActiveRecord::Base
validates :month, presence: true
validates :account_code, presence: true
end
transaction_test.rb
require 'test_helper'
class TransactionTest < ActiveSupport::TestCase
def setup
#@transaction = transactions(:one)
@transaction = Transaction.new(company: "Inc", month: Date.new(2015,6,15).to_s,
account_code: "80-32100-12-1201-60010",
description: "Salaries & Wages - Histo Gross 1", transaction_amount: 100000)
end
test "should be valid" do
assert @transaction.valid?
end
test "month should be present" do
@transaction.month = " "
assert_not @transaction.valid?
end
test "account_code should be present" do
@transaction.account_code = " "
assert_not @transaction.valid?
end
test "month cannot be a string" do #not sure if this is worth anything at all
@transaction.month = "xxxxxxxxxxxx"
assert_not @transaction.valid?
end
end
Rails生成的test auto(在我修改之前)很简单:
require 'test_helper'
class TransactionsControllerTest < ActionController::TestCase
test "should get show" do
get :show
assert_response :success
end
end
我将其修改为上面的当前版本,因为(我相信)测试应该首先创建一个事务,然后以某种方式引用 id。但我不确定我这样做是否正确。
我在 Whosebug 上看到过类似的问题。例如,这个问题很有帮助,但并没有完全把我带到那里 Count; find User with id= Minitest
注意:最初,我错误地设置了控制器。我把它设为单数"TransactionController",改成了复数"TransactionsController"。所以我也不得不在单元测试中更改名称。也许这对应用程序有影响。
所以...问题:
- 为什么单元测试失败而显示操作实际上是
在浏览器中工作? - 我如何更改单元测试中的代码以便它识别 我正在使用 transaction_params?
- 当我修改灯具时,是否必须修改灯具? unit_test 还是控制器?
好的,coorasse 的建议是使用 create!强制应用发现验证错误。初始错误(Couldn't find Transaction with 'id'= )已替换为新错误:
ActiveRecord::RecordInvalid: Validation failed: Month can't be blank, Account code can't be blank
这让我感到困惑,因为它让我想到了模型验证错误。为什么我在 ControllerTest 中收到模型验证错误?此外,transaction.rb 模型清楚地表明我已添加验证以满足 Month 和 AccountCode 的存在。
所以,我花了一些时间通读 "A Guide to Testing Rails Applications" Rails 指南 http://guides.rubyonrails.org/testing.html 在第 8 节中,它展示了如何在每个测试之前包含一个代码块。这是新的 transaction_controller_test
transaction_controller_test.rb(已修订)
require 'test_helper'
class TransactionsControllerTest < ActionController::TestCase
# called before every single test
def setup
@transaction = transactions(:one)
end
test "should get show" do
get :show, id: @transaction.id
assert_response :success
end
end
此后测试通过!不幸的是,Rails指南没有告诉我为什么我想在控制器测试中使用设置。如果任何聪明的 Rails 专家对此有一些指导,您的意见将不胜感激。