Rails 4:Helper方法无法检索关联记录

时间:2015-06-17 17:17:25

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

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

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

助手/ 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

我的规格..

规格/ 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 ==)

关系......

应用/模型/..

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

1 个答案:

答案 0 :(得分:1)

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

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