简体   繁体   中英

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:

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. 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! 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'. 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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