【问题标题】:Rails 4 STI Model Couldn't find with 'id' when running functional tests运行功能测试时,Rails 4 STI 模型找不到“id”
【发布时间】:2016-02-02 00:16:41
【问题描述】:

所以我有一个 Request 模型(我知道这是一个糟糕的名字),以及 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

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

【问题讨论】:

  • 在此处包含不起作用的测试代码,
  • @ArupRakshit 我在上面添加了测试,谢谢
  • 添加失败的测试、控制器和相关路由。
  • @DaveSchweisguth 如果在@myrequest = property_requests(:one) 行设置失败,甚至不会触及路由和控制器
  • 错误消息指出它发生在test_should_get_edit,你能把它也包括进去吗?如果您可以隔离它会更好(例如,尝试在仅包含相关方法的最小项目中获得相同的错误)。您可能会在此过程中发现问题本身。

标签: ruby-on-rails ruby ruby-on-rails-4 testing fixtures


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 欢迎来到 StackOverflow
    • @JulienNegrotto 这只是一个拼写错误,它实际上命名为 property_requests.yml
    • 感谢您的澄清。我找到了另一个解决方案,希望能解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多