Rails 4 STI 模型在运行功能测试时找不到'id'



所以我有一个请求模型(我知道这是一个糟糕的名字),以及 2 个单继承模型 TenantRequest 和 PropertyRequest。现在我有所有 3 个的固定装置。所以我为我的requests_controller和tenant_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

以下是property_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

如果您需要更多信息,请告诉我,我可以添加它。谢谢,

根据这篇博文,夹具文件与数据库表具有一对一的关系。每个子类都有文件可能会发生冲突。尝试将所有灯具放入requests.yml

您的夹具文件被命名为 property_request.yml(单数),而您在测试本身中调用property_requests(:one)(复数)。Rails Guides显示了给定复数名称的夹具文件,因此我将该文件重命名为property_requests.yml以使它们匹配并符合Rails的约定(并希望这就是问题所在)。

最新更新