Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rspec控制器测试失败,模型中出现参数错误_Ruby On Rails_Rspec_Callback_Constants - Fatal编程技术网

Ruby on rails rspec控制器测试失败,模型中出现参数错误

Ruby on rails rspec控制器测试失败,模型中出现参数错误,ruby-on-rails,rspec,callback,constants,Ruby On Rails,Rspec,Callback,Constants,我有这个问题,我正在测试我的控制器POST方法,我得到这个错误: ArgumentError: Missing invite relation 我有一个表单,它被发送,并且在调用_save other方法后被回调,该方法调用传递常量的其他方法。但被调用的方法调用验证,该验证在该常量上失败 下面是代码片段。型号: after_save :maintain_sender_attr def maintain_sender_attr self.affiliate_receive

我有这个问题,我正在测试我的控制器POST方法,我得到这个错误:

ArgumentError:
       Missing invite relation
我有一个表单,它被发送,并且在调用_save other方法后被回调,该方法调用传递常量的其他方法。但被调用的方法调用验证,该验证在该常量上失败

下面是代码片段。型号:

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
以下是常数:

NOTIFICATION_CONTENT_TYPES = [
  CONTENT_TYPE_INVITE = 'referral_invite'
]
下面是引发错误的地方:

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
最后,测试本身:

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

我无法理解到底出了什么问题。常量是在
config/environment.rb
上定义的,但感觉常量为空,因为如果常量为空,则会引发此错误!我必须存根或删除这个常量吗?

您实际上是在将通知内容类型定义为一个“常量数组,这些常量将根据其内容进行计算”。这意味着

NOTIFICATION_CONTENT_TYPES # => ["referral_invite"]
因此,要访问正确的常数,您必须执行以下操作:

NOTIFICATION_CONTENT_TYPES[0]
最好是以简洁的方式对其进行定义:

NOTIFICATION_CONTENT_TYPES = { content_type_invite: 'referral_invite' }
然后您可以使用访问正确的值

NOTIFICATION_CONTENT_TYPES[:content_type_invite]

它是这样的,因为这不是唯一的常量,它用于表单选项、选择生成和其他事情。只需调用CONTENT\u TYPE\u INVITE就可以在代码中正常工作。但是rspec测试失败了。。。。