Comments (12)
š What version of rspec-rails
are you using? There is no publically released rspec
4.0.0
(the highest release of rspec itself is 3.12.x
) but rspec-rails
s current release is 6.1.0
, I would recommend trying to upgrade rspec-rails
if you are using version 4.0.x
of that.
Closing because we don't support older versions of Rails (current support is the same as Rails itself, 6.1, 7.0 and 7.1) or older versions of rspec-rails
.
from rspec-rails.
I tried to upgrade the rspec-rails version to 5.0 but I am still getting the same error. I am stuck in the middle of a project because of this since a very long time. Please help me resolve this. @JonRowe
from rspec-rails.
Please provide a reproducible snippet like https://github.com/rspec/rspec-rails/blob/main/snippets/use_active_record_false.rb
from rspec-rails.
Please provide a reproducible snippet like https://github.com/rspec/rspec-rails/blob/main/snippets/use_active_record_false.rb
@pirj Can you please tell me how to use this ?
I could not understand.
from rspec-rails.
Those are self-contained scripts to reproduce issues like Rails bug templates https://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#create-an-executable-test-case
I wish we had a barebones one with some instructions, but we donāt
from rspec-rails.
@JonRowe I know you closed this but I think this is actually an issue with the latest.
I started a new project recently. I'm running:
rails = 7.1.2
ruby = 3.2.2
rspec-rails = 6.1.0
rspec ~> 3.12
I have this controller test to simulate what @raoehtesham03 was talking about:
(Note: the show_404
doesn't do any logic. My actual code in my project is a before_action
that checks for the presence of a particular subdomain, and otherwise renders the 404 file, but I didn't think that mattered for the purpose of highlighting this behavior.)
require "rails_helper"
RSpec.describe ApplicationController, type: :controller do
controller do
before_action :show_404
def index
render plain: "some content"
end
private
def show_404
render file: "public/404.html", status: 404, layout: false
end
end
it "renders the 404 page" do
get :index
expect(response).to have_rendered(file: "public/404.html", layout: false)
expect(response.status).to eq(404)
end
end
Here's the run:
āÆ bundle exec rspec spec/controllers/application_controller_spec.rb
ApplicationController
renders the 404 page (FAILED - 1)
Failures:
1) ApplicationController renders the 404 page
Failure/Error: expect(response).to have_rendered(file: "public/404.html", layout: false)
Expected [] to include "public/404.html".
# ./spec/controllers/application_controller_spec.rb:20:in `block (2 levels) in <top (required)>'
Finished in 0.17184 seconds (files took 2 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/application_controller_spec.rb:18 # ApplicationController renders the 404 page
I'm using have_rendered
instead of render_template
but it looks like it's an alias anyway.
Also, the render file: "public/404.html", status: 404, layout: false
in my project code works just fine when tested in the browser. It just seems the RSpect test doesn't quite work.
Cheers!
from rspec-rails.
What if you set a breakpoint inside show_404, will it stop there?
if you remove the rendered template expectation, will the response status expectation pass?
is it just for templates rendered in action filters?
Is it just for action filters defined inside controller do
?
What if you used the assert_template directly, would it work? Do we use it under the hood in our have_rendered matcher?
from rspec-rails.
Hi Phil! Thanks for your questions...
I tried this:
class PagesController < ApplicationController
def index
render file: "public/404.html", status: 404, layout: false
end
end
With this test:
require 'rails_helper'
RSpec.describe PagesController, type: :controller do
it "renders the 404 file" do
get :index
expect(response.not_found?).to be_truthy
end
end
as my baseline for your questions. Note that just testing the status code passes. Result:
# bundle exec rspec spec/controllers/pages_controller_spec.rb
PagesController
renders the 404 file
Finished in 0.14765 seconds (files took 2.08 seconds to load)
1 example, 0 failures
but when I also test the rendered template it fails.
In the test:
expect(response.not_found?).to be_truthy
expect(response).to have_rendered(file: "public/404.html", layout: false)
Result:
āÆ bundle exec rspec spec/controllers/pages_controller_spec.rb
PagesController
renders the 404 file (FAILED - 1)
Failures:
1) PagesController renders the 404 file
Failure/Error: expect(response).to have_rendered(file: "public/404.html", layout: false)
Expected [] to include "public/404.html".
# ./spec/controllers/pages_controller_spec.rb:7:in `block (2 levels) in <top (required)>'
What if you set a breakpoint inside show_404, will it stop there?
Yup. I tried that it and works fine. So it's running the before_action
. Also in my example above where it's just within a normal controller action (not a before_action
) it still hits. So it's not a flow issue.
if you remove the rendered template expectation, will the response status expectation pass?
Yes, as in the example above.
is it just for templates rendered in action filters?
Nope. It seems to be related to render file: "..."
. Something about it rendering a file not a template is where the bug is.
Is it just for action filters defined inside
controller do
?
Nope. See above.
What if you used the assert_template directly, would it work? Do we use it under the hood in our have_rendered matcher?
Fails similarly. In the test:
expect(response.not_found?).to be_truthy
assert_template file: "public/404.html"
With the result:
āÆ bundle exec rspec spec/controllers/pages_controller_spec.rb
PagesController
renders the 404 file (FAILED - 1)
Failures:
1) PagesController renders the 404 file
Failure/Error: assert_template file: "public/404.html"
Minitest::Assertion:
Expected [] to include "public/404.html".
So I think it's something specifically about using render file: "some/file"
that isn't assertable with render_template
or assert_template
(or have_rendered
, which is just an alias). š¤·
from rspec-rails.
@drsharp Instead of using this
expect(response).to have_rendered(file: "public/404.html", layout: false)
Try Below code snippet.
expect(response).to render_template { "public/404.html" }
from rspec-rails.
@drsharp Instead of using this expect(response).to have_rendered(file: "public/404.html", layout: false)
Try Below code snippet. expect(response).to render_template { "public/404.html" }
@raoehtesham03 thanks for the suggestion. It didn't work, though:
1) PagesController renders the 'index' action as the root path
Failure/Error: expect(response).to render_template { "public/404.html" }
ArgumentError:
wrong number of arguments (given 0, expected 1..2)
It's interesting because assert_template file: "some/file"
is very much supported:
https://github.com/rails/rails-controller-testing/blob/c203673f8011a7cdc2a8edf995ae6b3eec3417ca/lib/rails/controller/testing/template_assertions.rb#L97-L98
so I don't know why it's not working via rspec-rails. š¤·
from rspec-rails.
If assert_template doesnāt work, thereās not much we can do, as we usually just wrap Railsā assertions.
Youāre left to debug Rails testing code I believe.
from rspec-rails.
If assert_template doesnāt work, thereās not much we can do, as we usually just wrap Railsā assertions. Youāre left to debug Rails testing code I believe.
Thanks Phil. Yeah, I'm guessing at this point it's a problem with assert_template
itself, and not an rspec-rails
problem. š¤· Thanks for your thoughts and help here. I'll see about diving in with the Rails testing code.
from rspec-rails.
Related Issues (20)
- rails 7.1 stub_template issue: undefined method `unshift' for #<ActionView::PathSet HOT 4
- Wrong variable name when generate scaffold with namespace & model-name options HOT 1
- Providing JSON data no longer works in request specs with Rails 7.1 HOT 6
- Template stubs are not removed in Rails 7.1. HOT 4
- Rails 7.1 hangs in github actions when use_transactional_fixtures = true HOT 7
- "expect to receive with" throws error when switching to Rails 7.1 HOT 17
- Rails/RedundantActiveRecordAllMethod false positive HOT 1
- Rails 7.1 upgrade: using deprecated preview_path= HOT 3
- Rails 7.1 raise WrongScopeError on take_failed_screenshot HOT 3
- be_routable not working in controller spec with Rails 7.1 HOT 2
- Wrap examples with Rails executor HOT 2
- Fixtures for namespaced models don't seem to work in rspec-rails 6.1.0 HOT 6
- Even if specify the use of TestUnit, it will be overwritten by Minitest and cannot be used. HOT 6
- stub_template isn't working as expected on latest version HOT 1
- View example group inserts controller path into lookup_context in the wrong order HOT 2
- :git => "git://github.com/rspec/rspec.git" Not working anymore HOT 3
- rspec describe/context label within loop not woking HOT 5
- Controllers specs don't currently work on the Rails main branch (8.0.0.alpha) HOT 5
- Rack status code name changes breaks HttpStatusMatcher HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rspec-rails.