如何使用Rspec / Rails 5.2在分层父子关系中测试子创建?

问题描述 投票:0回答:1

我的应用程序管理BusinessFlow,只能在父BusinessArea中创建。 BusinessFlows控制器中的“新”方法是:

  def new
    @business_area = BusinessArea.find(params[:business_area_id])
    @business_flow = BusinessFlow.new
  end

用于测试的工厂工作正常,并创建了父BusinessArea和预期的BusinessFlow:

FactoryBot.define do
  factory :business_flow do
    association :parent,  factory: :business_area
    playground_id       {0}
    name                {"Test Business Flow"}
    code                {Faker::Code.unique.rut}
    description         {"This is a test Business Flow used for unit testing"}
    created_by          {"Fred"}
    updated_by          {"Fred"}
    owner_id            {0}
    responsible_id      {0}
    deputy_id           {0}
    organisation_id     {0}
    status_id           {0}
  end
end

但是当测试是否可以显示'new'视图时,由于缺少参数,控制器会引发错误[:business_area_id]:

 ActiveRecord::RecordNotFound:
   Couldn't find BusinessArea without an ID

这里是功能测试脚本:

require 'rails_helper'

RSpec.describe BusinessFlow, type: :request do
  include Warden::Test::Helpers

  describe "Business Flows pages: " do
    let(:bf) {FactoryBot.create(:business_flow)}

    context "when not signed in " do
      it "should propose to log in when requesting index view" do
        get business_flows_path
        follow_redirect!
        expect(response.body).to include('Sign in')
      end
      it "should propose to log in when requesting new view" do
        get new_business_flow_path
        follow_redirect!
        expect(response.body).to include('Sign in')
      end
      it "should propose to log in when requesting edit view" do
        get edit_business_flow_path(bf)
        follow_redirect!
        expect(response.body).to include('Sign in')
      end
      it "should propose to log in when requesting show view" do
        get business_flow_path(bf)
        follow_redirect!
        expect(response.body).to include('Sign in')
      end
    end
    context "when signed in" do
      before do
        get "/users/sign_in"
        test_user = FactoryBot.create(:user)
        login_as test_user, scope: :user
      end
      it "should display index" do
        get business_flows_path
        expect(response).to render_template(:index)
      end
      it "should display new view" do
        get new_business_flow_path(bf.parent.id)
        expect(response).to render_template(:_form)
      end
      it "should display edit view" do
        get edit_business_flow_path(bf)
        expect(response).to render_template(:_form)
      end
      it "should display show view" do
        get business_flow_path(bf)
        expect(response).to render_template(:show)
      end
    end
  end
end

仅在“登录时”上下文中的“新”方法测试失败。您能帮我解决这个问题吗?

非常感谢!

ruby-on-rails rspec factory-bot
1个回答
0
投票
 it "should display new view" do
        get new_business_flow_path(business_area_id: bf.parent)
        expect(response).to render_template(:_form)
      end

Rails认为您传递的id是业务流程路径的一部分(并且不是),我认为它需要作为参数传递。

© www.soinside.com 2019 - 2024. All rights reserved.