Skip to content

Failing tests in generated request spec from scaffold #2463

Closed
@panSarin

Description

@panSarin

Steps to reproduce

Install rspec in my rails 6.1.1 and above project

rails g scaffold product

Expected behavior:

Generated specs are passing when we fill in params.

Actual behavior

If we add any validation to Product model, and then run generated test:

  context "with invalid parameters" do
      it "does not create a new Product" do
        expect {
          post products_url, params: { product: invalid_attributes }
        }.to change(Product, :count).by(0)
      end

      it "renders a successful response (i.e. to display the 'new' template)" do
        post products_url, params: { product: invalid_attributes }
        expect(response).to be_successful
      end

  end

we get

Failure/Error: expect(response).to be_successful
expected #<ActionDispatch::TestResponse:0x00007f886d3b3808 @mon_data=#<Monitor:0x00007f886d3b37b8>, @mon_data_...true}, @request=#<ActionDispatch::Request PATCH "http://www.example.com/products/12" for 127.0.0.1>>.successful? to be truthy, got false
because since rails 6.1.1 generated controller looks like:

def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: "Product was successfully created." }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

Since that change ( responding with 422 unprocessabe_entity) was introduced in rails 6.1.1 I assume that this behavior is related to not updated scaffold methods for rspec. Or maybe tests are still fine, and introducing that change by default in rails wasn't intended? I am more than happy to prepare PR to fix it in code responsible for generating test or controller, but not sure what is a proper approach there ( i would vote for updating tests since 422 sounds more like a proper response than 200).

System configuration
Rails version: 6.1.1

Ruby version: 2.7.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions