1

I am very new to rails and I am writing test cases for my rails application. Suppose I have a code like this -

class EmailController < ApplicationController

  def new
    render :layout => false
  end

  def create
    begin
      process_email = ProcessEmail.new(params)
      process_email.perform
    rescue EmailHelper::HtmlSanitizerTimeoutError, EmailHelper::NokogiriTimeoutError => e
      NewRelic::Agent.notice_error(e, account_id: Account.current.id, subject: params[:subject])
      process_email.mark_email_on_timeout
    end
    render layout: 'email'
  end

 def update 
   ----
 end

I have written test cases for create and update. But how do I write test cases that will execute the rescue part??

This is my db (class abc < ActiveRecord::Base) this is my controller class (class abcsController < ApiApplicationController).

I have written minitest like this `

def test_raise_exception 
    params = {"a":"b"} 
    item = abc.new(params) 
    raises_exception = -> { raise ArgumentError.new } 
    item.stub :create, raises_exception do 
    assert_raises(ArgumentError) { post :create } 
    end 
end`

This is error which is coming -

NameError: NameError: undefined method create' for class

  • 1
    Does this answer your question? [How do I stub a method to raise an error using Ruby MiniTest?](https://stackoverflow.com/questions/10990622/how-do-i-stub-a-method-to-raise-an-error-using-ruby-minitest) – Roman Alekseiev Apr 20 '20 at 08:18
  • This is my db (class abc < ActiveRecord::Base) this is my controller class (class abcsController < ApiApplicationController). I have written minitest like this def test_raise_exception params = {"a":"b"} item = abc.new(params) raises_exception = -> { raise ArgumentError.new } item.stub :create, raises_exception do assert_raises(ArgumentError) { post :create } end end – Pushpendra Singh Apr 20 '20 at 09:13
  • @RomanAlekseiev its giving errors. NameError: NameError: undefined method `create' for class `#' – Pushpendra Singh Apr 20 '20 at 09:13
  • Please add details by editing the question instead of comments. They are not readable. – max Apr 20 '20 at 13:06

0 Answers0