简体   繁体   中英

Testing Controller Action

Im having issues testing the controller action for my POST response via the FRESHDESK API

def post_inquiry
  @cert_inquiry = Form.new(params[:cert_inquiry])
  if @cert_inquiry.valid?
    @cert_inquiry.post_tickets(subject_title)
    flash[:success] = 'Message sent! Thank you for contacting us.'
    redirect_to '/flat-roof-mounting/resources'
  else
    flash[:alert] = 'Please fill in the required fields'
    redirect_to root_path
  end
end

heres my spec

let!(:cert_params) do
  {
    "subject"=>"test",
    "email"=>"test@test.com",
    "custom_field"=>{"cert_letter_state_28445"=>"CA"},
    "description"=>"test"
  }
end

describe 'POST #post_inquiry' do
  def do_post
    post :post_inquiry, cert_inquiry: cert_params
  end

  before{ expect(Form).to receive(:new).with(cert_params) }

  it do
    do_post
    expect(response).to redirect_to '/flat-roof-mounting/resources'
  end
 end

heres my failures (amongst the many others in life)

 Failure/Error: post :post_inquiry, cert_inquiry: cert_params
 NoMethodError:
   undefined method `valid?' for nil:NilClass

its calling through to the Freshdesk API.....do I have to stub out the api call somehow? If so how? i Have like api keys urls and all that good stuff but not sure what I'm suppose to do with it

Try changing the following:

before { expect(Form).to receive(:new).with(cert_params) }

to:

before { expect(Form).to receive(:new).with(cert_params).and_call_original }

and see if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM