0

I'm creating an Rspec controller test for a create action that takes parameters for two objects, story and ku. Story validates presence of title and large_cover_url. Not sure why rspec throws an argument error when passing invalid parameters, and could use some insight on this issue.

Here is the error:

Failure/Error: post :create, story: {title: nil, large_cover_url: "present"}, ku: Fabricate.attributes_for(:ku)
     ArgumentError:
       wrong number of arguments (0 for 2)
     # ./app/controllers/stories_controller.rb:18:in `create'
     # ./spec/controllers/stories_controller_spec.rb:59:in `block (4 levels) in <top (required)>'

Here is the spec:

context "failed story creation" do
    let(:bob) { Fabricate(:user) }
    before { sign_in_user(bob) }

    it "does not create a new story" do
        post :create, story: {title: nil, large_cover_url: "present"}, ku: Fabricate.attributes_for(:ku)
        expect(Story.count).to eq(0)
    end
end

Here is the controller action:

def create
    @story = Story.new(params[:story])
    @ku = Ku.new(params[:ku])
  if @story.save && @ku.save
    @story.update_column(:user_id, current_user.id)
    @ku.update_column(:user_id, current_user.id)
    @story.kus << @ku
    redirect_to story_path(@story), flash: {success: "Your story was published."}
  else
    flash[:error] = "#{@story.errors.full_message.join(', ')}"
    render :new
  end
end

1 Answer 1

1

The method full_message accepts as parameters the name of an attribute an the error string, so when you do flash[:error] = "#{@story.errors.full_message.join(', ')}", you are calling full_message without any parameters. You should use full_messages instead that returns an array with all the error messages.

ActiveModel::Errors api ref

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Pablo, that was spot on!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.