测试控制器动作

时间:2016-03-15 19:07:12

标签: ruby-on-rails ruby rspec

我遇到了通过FRESHDESK API测试我的POST响应的控制器操作的问题

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

继承人我的规范

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

继承我的失败(在生活中的许多其他人中)

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

它调用Freshdesk API .....我是否必须以某种方式删除api调用?如果是这样的话?我喜欢api key urls和所有好东西,但不确定我想用它做什么

1 个答案:

答案 0 :(得分:0)

尝试更改以下内容:

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

为:

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

看看它是否有效。