Ruby on rails 用Rspec测试Mailchimp 3.0和Gibbon 2.x

Ruby on rails 用Rspec测试Mailchimp 3.0和Gibbon 2.x,ruby-on-rails,rspec,mailchimp,gibbon,Ruby On Rails,Rspec,Mailchimp,Gibbon,我有一个rails 4.2应用程序,它通过MailChimp使用Gibbon gem注册新闻稿 这是我的初始值设定项: Gibbon::Request.api_key = ENV['MAILCHIMP_API_KEY'] Gibbon::Request.timeout = 15 Gibbon::Request.throws_exceptions = false 以下是user.rb中的相关方法: # returns the mailchimp member if one exists fo

我有一个rails 4.2应用程序,它通过MailChimp使用Gibbon gem注册新闻稿

这是我的初始值设定项:

Gibbon::Request.api_key = ENV['MAILCHIMP_API_KEY']
Gibbon::Request.timeout = 15
Gibbon::Request.throws_exceptions = false
以下是user.rb中的相关方法:

  # returns the mailchimp member if one exists for @user.email
  def mailchimp_user
    gb = Gibbon::Request.new(api_key: ENV['MAILCHIMP_API_KEY'])
    gb.lists(ENV['MAILCHIMP_LIST_ID']).members(Digest::MD5.hexdigest("#{self.email.downcase}")).retrieve
    rescue Gibbon::MailChimpError => e
    return nil, :flash => { error: e.message }
  end

  def mailchimp_member_id
    if self.mailchimp_user.kind_of?(Array)
      return nil
    elsif self.mailchimp_user.kind_of?(Hash)
      self.mailchimp_user["id"]
    end
  end

  # returns the mailChimp status of the user  
  def mailchimp_status
    if self.mailchimp_user.kind_of?(Array)
      return nil
    elsif self.mailchimp_user.kind_of?(Hash)
      self.mailchimp_user["status"]
    end
  end

  # syncs Mailchimp status to EBW, subscribing and unsubscribing users as appropriate. potential MC status includes
  # 'subscribed', 'unsubscribed', 'pending' and 'cleaned'
  def add_to_mailchimp
    gb = Gibbon::Request.new(api_key: ENV['MAILCHIMP_API_KEY'])
    if self.subscribed? # if the user has checked the "subscribed" checkbox
      if self.mailchimp_status.present? # if MailChimp recognizes the email address
        gb.lists(ENV['MAILCHIMP_LIST_ID']).members(Digest::MD5.hexdigest("#{self.email.downcase}")).update(body: { status: "subscribed" }) #subscribe user 
      elsif self.mailchimp_status.nil? # if MailChimp doesn't have a user for the email address
        gb.lists(ENV['MAILCHIMP_LIST_ID']).members.create(body: { # create a MailChimp pending subscriber
          email_address: "#{self.email}",
          status: "pending", # setting this to 'subscribed' will remove double optin
          merge_fields: {FNAME: "#{self.name}"}
          })
      end

  # unsubscribe user if box is unchecked but mailchimp has user as subscribed or pending
    else
      unless self.mailchimp_user.kind_of?(Array)
        gb.lists(ENV['MAILCHIMP_LIST_ID']).members(self.mailchimp_member_id).update(body: { status: "unsubscribed" })  
      end
    end
  end
我仍在学习Rspec,在将其应用于Gibbon::MailChimp的上下文时遇到了困难。我想测试一下,根据用户的当前状态,订阅和取消订阅是否能像预期的那样工作。e、 g.取消订阅的用户与需要创建MailChimp订阅的新用户的待遇不同