简体   繁体   English

rspec控制器测试因模型中的参数错误而失败

[英]rspec controller test fails with argument error in model

I have this problem, i am testing my controllers POST method and i get this error: 我有这个问题,我正在测试我的控制器的POST方法,并且出现此错误:

ArgumentError:
       Missing invite relation

I have form which is sent and with callback after_save other method is called which calls other method where constant is passed. 我有发送的形式,并带有after_save回调,调用了另一个方法,该方法调用传递常量的其他方法。 But called method calls validation which fails on that constant. 但是调用的方法将调用验证,该验证在该常量上失败。

Here is the snippets of code. 这是代码片段。 Model: 模型:

after_save :maintain_sender_attr

def maintain_sender_attr
    self.affiliate_receivers.each do |count|
      unless count.email == ""
        send_email(count.email)
      end
    end
  end

def send_email(to)
      @resource_invite = InviteSender::NewInvite.new(self, to, CONTENT_TYPE_INVITE)
      @resource_invite.body_html = prepare(@resource_invite.invite.body_html)
      @resource_invite.send
    end

    def prepare body_html
      context_objects.each {|key, value| body_html.gsub! "[#{key}]", value}
      body_html
    end

here is the constant: 这是常量:

NOTIFICATION_CONTENT_TYPES = [
  CONTENT_TYPE_INVITE = 'referral_invite'
]

and here is where the error is raised: 这是引发错误的地方:

class InviteSender::Base
  attr_accessor :missing_fields, :body_html, :invite, :owner, :to

  # Initialize new sender instance
  # @notification [Notification, String] notification or notification content_type
  def initialize owner, to, invite
    @owner = owner
    @to = to
    if invite.is_a? Invite
      @invite = invite
    else
      @invite = Invite.find_by(content_type: invite)
    end

    validate

    load_body_html_template
  end

def validate
    raise ArgumentError, "Missing owner relation" if @owner.nil?
    raise ArgumentError, "Missing invite relation" if @invite.nil?
    raise ArgumentError, "Missing to relation" if @to.nil?
    raise NotImplementedError, "Missing #locale method" unless respond_to? :locale
  end

lastly the test itself: 最后是测试本身:

describe "with valid attributes" do
      it "creates new sender and there affiliated receivers" do
        expect{
          post :create, node_id: @node.id, locale: @node.locale, affiliate_sender: FactoryGirl.attributes_for(:affiliate_sender, :affiliate_receivers_attributes =>{"0"=>{"email"=>"test@test.com"}}), format: :json
        }.to change(AffiliateSender, :count).by(1)
      end

      it "it return success json" do
        post :create, node_id: @node.id, locale: @node.locale , affiliate_sender: FactoryGirl.attributes_for(:affiliate_sender, :affiliate_receivers_attributes =>{"0"=>{"email"=>"test@test.com"}}), format: :json
        response.status.should eq(200)
      end
    end

I can't undesrtand what exactly goes wrong. 我无法取消错误到底是什么。 Constant is defined on config/environment.rb but it feels like constant is empty, because this error is raised if constant is empty! 常量是在config/environment.rb上定义的,但是感觉常量为空,因为如果常量为空,则会引发此错误! Do i have to stub or moch this constant? 我是否必须存根或移动该常数?

You are actually defining NOTIFICATION_CONTENT_TYPES as an array of 'Constants which get evaluated to their content'. 您实际上是在将NOTIFICATION_CONTENT_TYPES定义为“对其内容进行评估的常量”的数组。 This means that 这意味着

NOTIFICATION_CONTENT_TYPES # => ["referral_invite"]

So to access the right Constant you have to do: 因此,要访问正确的常量,您必须执行以下操作:

NOTIFICATION_CONTENT_TYPES[0]

Better would be to define it in a concise way: 最好以简洁的方式定义它:

NOTIFICATION_CONTENT_TYPES = { content_type_invite: 'referral_invite' }

And then you can access the right value with 然后,您可以使用

NOTIFICATION_CONTENT_TYPES[:content_type_invite]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM