Skip to content

Failing tests in generated request spec from scaffold #2463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
panSarin opened this issue Feb 18, 2021 · 3 comments
Closed

Failing tests in generated request spec from scaffold #2463

panSarin opened this issue Feb 18, 2021 · 3 comments

Comments

@panSarin
Copy link

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

@panSarin
Copy link
Author

panSarin commented Feb 18, 2021

Pull request that I opened to fix that: #2466

@pirj
Copy link
Member

pirj commented Mar 27, 2022

If you set a breakpoint after @post.save or make it a bang-save!, or log @post.errors.messages, what the error it would be?

@panSarin
Copy link
Author

panSarin commented Apr 1, 2022

Ugh, i don't have this setup on my current machine atm. But I think the issue is solved for the newest rails in https://github.com/rspec/rspec-rails/pull/2585/files and that one is closed. So I think we are fine.

@panSarin panSarin closed this as completed Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants