我的控制器的显示操作未通过单元测试。我在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
尽管出现此错误,但当我使用此网址时,视图已成功在浏览器中生成页面: 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自动生成。我是铁杆新手,不得不承认我并不真正了解灯具。
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
在我的路线文件中,我将根设置为索引操作。我计划通过网址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自动生成的测试(在我修改之前)非常简单:
require 'test_helper'
class TransactionsControllerTest < ActionController::TestCase
test "should get show" do
get :show
assert_response :success
end
end
我将它修改为上面的当前版本,因为(我相信)测试应首先创建一个事务,然后以某种方式引用id。但我不确定我是否正确地做到了这一点。
我在Stackoverflow上看到过类似的问题。例如,这个问题很有帮助,但并没有让我在那里 Count; find User with id= Minitest
注意:最初,我错误地设置了控制器。我将其设置为单个“TransactionController”并将其更改为复数“TransactionsController”。所以我不得不在单元测试中更改名称。也许这对应用程序产生了影响。
所以......问题:
答案 0 :(得分:0)
好的,所以coorasse建议使用create!强迫应用程序发现验证错误。初始错误(无法找到带有'id'=的事务)被替换为新错误:
ActiveRecord :: RecordInvalid:验证失败:月份不能为空, 帐户代码不能为空
这令人困惑,因为它让我想到了模型验证错误。为什么我在ControllerTest中遇到模型验证错误?此外,transaction.rb模型清楚地表明我已添加验证以满足Month和AccountCode的存在。
所以,我花了一些时间阅读RailsGuides上的“测试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
此后测试通过!不幸的是,RailsGuides没有告诉我为什么我想在控制器测试中使用setup。如果任何智能Rails专家对此有一些指导,那么您的意见将不胜感激。