Fred Benfield
Fred Benfield

Reputation: 5

stubbing controllers in rspec

I am trying to fathom controller testing, and with that, mocking and stubbing. I think I get the latter, however, not being so confident in my tests I am unwilling to place any faith in the current failing test I am about to explain.

RSpec.describe CorporateContactFormsController, :type => :controller do
    let!(:contact_form){ FactoryGirl.create :corporate_contact_form }
    let!(:params_new){ {corporate_contact_form: {}} }

  describe "POST create" do
    before do 
        CorporateContactForm.stub(:new).and_return(contact_form)
      get :new, params_new
    end

    it "returns http success" do
      expect(response).to be_success
    end
  end
  describe "POST create" do
    before do
        CorporateContactForm.stub(:new).and_return(contact_form)
    end
    describe "where all is good" do
      before do
        contact_form.stub(:save).and_return(true)
        post 'create', params_new
      end
      it "should set a flash notice" do
        flash[:notice].should_not be_nil
      end
      it "should redirect to edit_details page" do
        response.should redirect_to employees_path
      end
    end
  end

end

So here I am testing the controller which deals with form submissions. But here is where I am lost...

  def create
    if corporate_contact_form_params.present?
      @contact_form = CorporateContactForm.new(corporate_contact_form_params)
      if @contact_form.save
        if @contact_form.contact_type == CorporateContactForm::EMPLOYEE 
          redirect_to employees_path
        else
          redirect_to employers_path
        end
        flash[:notice] = 'Form submitted'
        return
      end
    end
    render new
  end

  private 

  def corporate_contact_form_params
    params.require(:corporate_contact_form).permit(:firstname, :surname, :contact_number, :email, :company_name, :method_of_payment, :number_of_employees, :comments, :contact_type)
  end

With my parameters set as above, I get the failing test

Failure/Error: post 'create', params_new
     ActionController::ParameterMissing:
       param is missing or the value is empty: corporate_contact_form

I really am struggling to understand what's going on here, why should this test fail, because its works in development. Why would the first describe "POST create test pass and not the next block?

Upvotes: 0

Views: 59

Answers (1)

BroiSatse
BroiSatse

Reputation: 44725

The error message is clear: param is missing or the value is empty: corporate_contact_form. The value you are passing under corporate_contact_form key is empty and require is not happy about that. Try changing your let! (should be let, eager load is obsolete in this case) to sth like:

let!(:params_new){ {corporate_contact_form: { firstname: ''} } }

EDIT:

This is how require method is defined:

def require(key)
  value = self[key]
  if value.present? || value == false
    value
  else
    raise ParameterMissing.new(key)
  end
end 

present method returns false for empty hash.

Upvotes: 1

Related Questions