Techioz Blog

Rspec メールガン API を作成する方法

概要

私は mailgun API https://documentation.mailgun.com/en/latest/api-sending.html#examples をフォローしています

def send_simple_message
  RestClient.post "https://api:YOUR_API_KEY"\
  "@api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
  :from => "Excited User <mailgun@YOUR_DOMAIN_NAME>",
  :to => "[email protected], YOU@YOUR_DOMAIN_NAME",
  :subject => "Hello",
  :text => "Testing some Mailgun awesomness!"
end

ApplicationServiceを入れてコントローラーを呼び出します

SendMailService.new(param['email']).call
enter code here

私の質問は、rspec でどのように作成または書き込みを行うかです。

解決策

テスト環境では、ポストリクエストから得られる結果を取得して、可能な限り現実的に指定します。

ただし、仕様内ではモックを使用できます。

allowed(RestClient).to accept(:post).and_return({whatever_you_want: ‘test’})

このような API に応じてコードをテストします。