mhplur
mhplur

Reputation: 218

Rspec method create error

i try to test method create in my controller:

def create
    @fund = Fund.new({started_at: Date.strptime(params[:fund].delete(:started_at), '%m/%d/%Y')}.merge(fund_params))
    if @fund.save
      flash[:alert] = "Fund #{@fund.name} saved!"
      redirect_to funds_path
    else
      flash[:error] = "Fund #{@fund.name} could not be saved"
      render :edit
    end
end

file spec:

it 'Can create a new fund' do
    fund = FactoryGirl.create(:fund)
    post :create
    expect(response).to redirect_to(funds_path)
end

And show this error:

NoMethodError: undefined method `delete' for nil:NilClass

The error is in this line, for the method 'delete':

 @fund = Fund.new({started_at: Date.strptime(params[:fund].delete(:started_at),...

i don't know how solve this problem, Thanks.

Upvotes: 0

Views: 59

Answers (1)

markets
markets

Reputation: 7033

As the error message says, that's because params[:fund] is nil. Try to send the proper parameters to the create action:

post :create, fund: { add_here_needed_params_for_this_action }

Example:

post :create, fund: { started_at: Date.today }

Upvotes: 1

Related Questions