Rails 4 STI 模型在 运行 功能测试时找不到 'id'
Rails 4 STI Model Couldn't find with 'id' when running functional tests
所以我有一个 Request 模型(我知道这是一个糟糕的名字),以及 2 个单一继承模型 TenantRequest 和 PropertyRequest。现在我有所有 3 个固定装置。所以我为我的 requests_controller 和我的租户_requests_controller 编写了功能控制器测试,它们都工作正常。但出于某种原因,我的 property_controller 测试显示每个设置都出现以下错误:
1) Error:
PropertyRequestsControllerTest#test_should_get_edit:
ActiveRecord::RecordNotFound: Couldn't find Request with 'id'=298486374
test/controllers/property_requests_controller_test.rb:12:in `block in <class:PropertyRequestsControllerTest>'
这是tenant_requests.yml:
one:
title: This is the title of the tenant request
body: This is the body
user: regular
email: jim@retail.com
type: TenantRequest
contact_name: Jim
这是我的 property_request.yml:
one:
title: This is the title of the property request
body: This is the body for property
user: broker
email: sue@broker.com
type: PropertyRequest
contact_name: Sue
budget: 1234
city: New York
region: Manhattan
created_at: now
updated_at: now
status: open
company: Walmart
contact_position: Boss
contact_phone: 555-555-5555
squarefeet: 12345
broker: true
parking: true
onsite_tour: true
part_of_town: Downtown
time_to_reach: 7pm
budget: 1234
这里是属性_requests_controller_test:
require 'test_helper'
class PropertyRequestsControllerTest < ActionController::TestCase
setup do
@regular = users(:jim)
@broker = users(:sue)
@analyst = users(:kev)
@admin = users(:lin)
sign_in :user, @analyst
@myrequest = property_requests(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:requests)
end
test "should get new" do
get :new
assert_response :success
end
test "should create request successfully" do
assert_difference('Request.count') do
post :create, request: { contact_name: 'Sue', body: 'this is the body', email: 'sue@broker.com', title: 'newly created property request', type: 'PropertyRequest' }
end
assert_redirected_to property_request_path(PropertyRequest.last)
end
如果您需要更多信息,请告诉我,我可以添加。谢谢,
您的夹具文件名为 property_request.yml
(单数),而您在测试本身中调用 property_requests(:one)
(复数)。 Rails Guides 显示给定复数名称的夹具文件,因此我将文件重命名为 property_requests.yml
以使它们匹配并符合 Rails 的约定(希望这就是问题所在)。
根据这个 blog post,fixture 文件与数据库表有 one-to-one 关系。每个 child class 都有文件可能会发生冲突。尝试将所有灯具放入 requests.yml
.
所以我有一个 Request 模型(我知道这是一个糟糕的名字),以及 2 个单一继承模型 TenantRequest 和 PropertyRequest。现在我有所有 3 个固定装置。所以我为我的 requests_controller 和我的租户_requests_controller 编写了功能控制器测试,它们都工作正常。但出于某种原因,我的 property_controller 测试显示每个设置都出现以下错误:
1) Error:
PropertyRequestsControllerTest#test_should_get_edit:
ActiveRecord::RecordNotFound: Couldn't find Request with 'id'=298486374
test/controllers/property_requests_controller_test.rb:12:in `block in <class:PropertyRequestsControllerTest>'
这是tenant_requests.yml:
one:
title: This is the title of the tenant request
body: This is the body
user: regular
email: jim@retail.com
type: TenantRequest
contact_name: Jim
这是我的 property_request.yml:
one:
title: This is the title of the property request
body: This is the body for property
user: broker
email: sue@broker.com
type: PropertyRequest
contact_name: Sue
budget: 1234
city: New York
region: Manhattan
created_at: now
updated_at: now
status: open
company: Walmart
contact_position: Boss
contact_phone: 555-555-5555
squarefeet: 12345
broker: true
parking: true
onsite_tour: true
part_of_town: Downtown
time_to_reach: 7pm
budget: 1234
这里是属性_requests_controller_test:
require 'test_helper'
class PropertyRequestsControllerTest < ActionController::TestCase
setup do
@regular = users(:jim)
@broker = users(:sue)
@analyst = users(:kev)
@admin = users(:lin)
sign_in :user, @analyst
@myrequest = property_requests(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:requests)
end
test "should get new" do
get :new
assert_response :success
end
test "should create request successfully" do
assert_difference('Request.count') do
post :create, request: { contact_name: 'Sue', body: 'this is the body', email: 'sue@broker.com', title: 'newly created property request', type: 'PropertyRequest' }
end
assert_redirected_to property_request_path(PropertyRequest.last)
end
如果您需要更多信息,请告诉我,我可以添加。谢谢,
您的夹具文件名为 property_request.yml
(单数),而您在测试本身中调用 property_requests(:one)
(复数)。 Rails Guides 显示给定复数名称的夹具文件,因此我将文件重命名为 property_requests.yml
以使它们匹配并符合 Rails 的约定(希望这就是问题所在)。
根据这个 blog post,fixture 文件与数据库表有 one-to-one 关系。每个 child class 都有文件可能会发生冲突。尝试将所有灯具放入 requests.yml
.