开发者

Ruby On Rails Function Testing :index with a polymorphic model

开发者 https://www.devze.com 2023-02-15 15:27 出处:网络
I have a polymorphic model called Address, I am trying to currently write some basic function tests for this model and controller. For the controller I am at a loss on how to go about this. For exampl

I have a polymorphic model called Address, I am trying to currently write some basic function tests for this model and controller. For the controller I am at a loss on how to go about this. For example I have another model called Patient, each Patient will have an address, so i have started writing the following function test, but i have no idea how to use "get" with a nested polymorphic resource. Now I was able to find some polymorphic test information on Fixtures here: http://api.rubyonrails.org/classes/Fixtures.html but this will not help me test against the index. Any help is much appreciated im at a t开发者_JAVA百科otal and complete loss here.

FILE: test/functional/addresses_controller_test.rb

require 'test_helper'
class AddressesControllerTest < ActionController::TestCase
  setup do
    @address = addresses(:of_patient)
    @patient = patients(:one)
    activate_authlogic
  end

  test "patient addresses index without user" do
    get :index  <<<<<<<<<<<< what goes here????
    assert_redirected_to :login
  end
end


Assuming your controller is setup the way I think it might be:

def index
  if @current_user
    @addresses = @current_user.addresses.all
  else
    redirect_to login_path
  end
end

Then the test will probably look like this:

test "patient addresses index without user" do
  get :index, :patient_id => @patient.id
  assert_redirected_to :login
end

test "patient addresses with user" do
  @current_user = @patient
  get :index, :patient_id => @patient.id
  assert_response :success
end

The thing to keep in mind is that the index method needs the patient_id to process.

0

精彩评论

暂无评论...
验证码 换一张
取 消