2016-03-15 274 views
0

林在进行测试的通过的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"=>"[email protected]", 
    "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密钥URL和所有的好东西,但不知道我是假设用它做

回答

0

尝试改变如下:

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

到:

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

,看看它是否有效。

+0

首先感谢您的回复.....其次,当我添加.and_call_original我得到一个NoMethodError: 未定义的方法'[]'为零:NilClass –