dimanche 17 mai 2015

stubs MailChimp api call for functional testing

I'm trying to test a MailChimp subscription to an specific list:

test/functional/some_controller_test.rb

require 'test_helper'

class SomeControllerTest < ActionController::TestCase
  test "invalid signup" do
    Gibbon.stubs(:subscribe).raises(Gibbon::MailChimpError, 500)
    post :signup, {:EMAIL => "invalid_email"}
    assert_response 500
  end

  test "valid signup" do
    Gibbon.stubs(:subscribe).returns(200)
    post :signup, {:EMAIL => "test@email.com"}
    assert_response 200
  end
end

controllers/some_controller.rb

class SomeController < ApplicationController
  def signup
    begin
      gb = Gibbon::API.new
      resp = gb.lists.subscribe(
        :id => ENV["key_list"],
        :email => {:email => "#{params[:EMAIL]}"}
      )
      render :status => :ok, :json => resp
    rescue Gibbon::MailChimpError => e
      render :status => :internal_server_error, :json => {error: e, message: e.message}
    end
  end
end

But I think that I missing something here because the tests are not passing.

Aucun commentaire:

Enregistrer un commentaire