Karl
Karl

Reputation: 595

Rails 4 STI Model Couldn't find with 'id' when running functional tests

So I have a Request model (I know it's a terrible name), and 2 single inherited models TenantRequest and PropertyRequest. Now I have fixtures for all 3. So I wrote functional controller tests for my requests_controller and my tenant_requests_controller, which both work fine. But for some reason, my property_controller tests show me the following error for every setup:

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>'

This is the tenant_requests.yml:

one:
  title: This is the title of the tenant request
  body: This is the body
  user: regular
  email: [email protected]
  type: TenantRequest
  contact_name: Jim

Here is my property_request.yml:

one:
  title: This is the title of the property request
  body: This is the body for property
  user: broker
  email: [email protected]
  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

Here is the 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: '[email protected]', title: 'newly created property request', type: 'PropertyRequest' }
    end

    assert_redirected_to property_request_path(PropertyRequest.last)
  end

If you need more information, please let me know and I can add it. Thank you,

Upvotes: 1

Views: 256

Answers (2)

Julien Negrotto
Julien Negrotto

Reputation: 458

According to this blog post, fixture files have a one-to-one relationship with database tables. There could be a conflict occurring from having files for each child class. Try placing all fixtures into requests.yml.

Upvotes: 1

Julien Negrotto
Julien Negrotto

Reputation: 458

Your fixture file is named property_request.yml (singular), while you're calling property_requests(:one) (plural) in the test itself. The Rails Guides show fixture files given pluralized names, so I would rename the file to property_requests.yml to make them match and conform to Rails' conventions (and hope that's the issue).

Upvotes: 1

Related Questions