【问题标题】:Rails 4: Helper method not retrieving associated recordsRails 4:辅助方法不检索关联记录
【发布时间】:2015-09-03 01:54:03
【问题描述】:

我有一个自定义帮助方法,其唯一目的是计算将在视图中显示的关联记录的数据。在 Rails 控制台中,我可以添加 Campaign.find(1).plan.subscribers 并检索订阅者列表,但我无法在自定义 Helper Method 中执行此操作。

这是辅助方法.. (注意:您可以查看我尝试打印出campaign.plan.subscribers 以查看它是否在循环之前注册...不是)

helpers/application_helper.rb

module ApplicationHelper

    def current_recipients(campaign)
        @target_program = campaign.program.name
        @recipients = 0

        #p campaign.plan.subscribers

        campaign.plan.subscribers.each do |s|
            case @target_program
                when "Caspian Star" && s.star?
                    @recipients += 1
                when "STIX" && s.stix?
                    @recipients += 1
                when "PPCI" && s.ppci?
                    @recipients += 1
                else
                    @recipients += 0
                end
        end
        @recipients
    end
end

我的规格..

spec/helpers.application_helper_spec.rb

require "rails_helper"

RSpec.describe ApplicationHelper, :type => :helper do

    describe "Counting campaign recipients" do
        subject(:campaign) { create(:campaign, plan: plan, program: program) }
        let(:program) { create(:program) }
        let(:plan) { create(:plan) }

        it "finds subscribers under campaign plan and program" do
            expect(helper.current_recipients(campaign)).to eq 3
        end
    end
end

失败

  1) ApplicationHelper Counting campaign recipients finds subscribers under campaign plan and program
     Failure/Error: expect(helper.current_recipients(campaign)).to eq 3

       expected: 3
            got: 0

       (compared using ==)

关系..

app/models/..

Class Campaign < ActiveRecord::Base
    belongs_to :program
    belongs_to :plan
end

Class Plan < ActiveRecord::Base
    has_many :campaigns
    has_many :subscribers, through: :plannables
    has_many :plannables
end

Class Program < ActiveRecord::Base
    has_many :campaigns
end

class Subscriber < ActiveRecord::Base
    has_many :plans, through: :plannables
    has_many :plannables
end

class Plannable < ActiveRecord::Base
    belongs_to :plan
    belongs_to :provider
end

编辑

这是按要求添加的工厂。

FactoryGirl.define do

    factory :campaign do |x|
        x.sequence(:name) { |y| "Q6 201#{y}" }
        x.sequence(:comment) { |y| "JIRA OI-6#{y}" }
        channels ["Folder", "Fax"]
    end

    factory :program do
        name "Caspian Star"
    end

    factory :plan do
        name "Third Storm Connect"
    end
end

【问题讨论】:

  • 当您创建测试活动时,您是如何链接程序和订阅者的?除非你有没有向我们展示的工厂魔法,否则看起来不会发生这种情况。
  • @steveklein - 订阅者有一个布尔字段表示每个程序选择加入/退出状态,因此case 语句将程序名称与每个订阅者的布尔字段匹配,如果布尔字段是true,则收件人计数 += 1。
  • 不,我问的是您的测试设置。您在哪里将您正在创建的活动链接到程序以及该程序与订阅者?也许你可以在你的 OP 中展示你的工厂。
  • 谢谢 - 我看到了工厂。您在哪里定义测试活动的程序或计划?在您的主题行中,“程序”和“计划”是如何初始化的?
  • @steveklein - 我用工厂文件更新了帖子。就测试而言,我不太确定这是如何完成的,因此我将种子数据添加到订阅者的测试数据库中,并根据现有计划命名该计划。而不是let(:plan) { create(:plan) } 我尝试let(:plan) { Plan.find(2) } 以获取现有计划但收到错误。我是个新手,总体上仍然围绕着 TDD 和 Factory Girl。

标签: ruby-on-rails ruby ruby-on-rails-4 rspec helper


【解决方案1】:

问题最终出现在 case 语句中,该语句没有引发任何明显的错误。它对when 条件下的&amp;&amp; 反应不佳,所以我将它们放在下面,现在它似乎工作正常。这是代码现在的样子..

module ApplicationHelper

    def current_recipients(campaign)
        @target_program = campaign.program.name
        @recipients = 0

        campaign.plan.subscribers.each do |s|
            case @target_program
                when "Caspian Star"
                    s.star?
                    @recipients += 1
                when "STIX"
                    s.stix?
                    @recipients += 1
                when "PPCI"
                    s.ppci?
                    @recipients += 1
                else
                    @recipients += 0
                end
        end
        @recipients
    end
end

根据@steveklein 在聊天会话中提出的建议,通过以这种方式将我的对象关联链接在一起,我能够通过测试。

describe "Counting campaign recipients" do
    it "finds subscribers under chosen campaign plan and program" do
        campaign = create(:campaign)
        plan = create(:plan)
        campaign.plan = plan
        campaign.program = create(:program)

    3.times do
        subscriber = create(:subscriber, star: true)
        subscriber.plans << plan
    end

        expect(helper.current_recipients(campaign)).to eq 3
    end
end

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多