Ruby On Rails 功能测试:具有多态模型的索引

发布于 2024-10-20 16:18:30 字数 790 浏览 0 评论 0原文

我有一个名为 Address 的多态模型,我目前正在尝试为该模型和控制器编写一些基本功能测试。对于控制器来说,我不知道如何解决这个问题。例如,我有另一个名为 Patient 的模型,每个 Patient 都会有一个地址,所以我开始编写以下函数测试,但我不知道如何将“get”与嵌套多态资源一起使用。现在我可以在这里找到一些有关 Fixtures 的多态测试信息: http://api.rubyonrails.org /classes/Fixtures.html 但这无助于我对索引进行测试。非常感谢任何帮助,我在这里完全迷失了。

文件:测试/功能/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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-10-27 16:18:31

假设您的控制器按照我认为的方式设置:

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

那么测试可能如下所示:

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

需要记住的是,index 方法需要 Patient_id 来处理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文