I'm trying to test a Rails controller concern (though I'm not sure this fact is at all related to my issue) in RSpec, so I figured I'd use an anonymous controller. Here's my setup:
describe MyConcern do
controller do
include MyConcern
def edit
puts "Checkpoint!"
@value = concern_method
render text: "test edit method called"
end
end
it "should call concern_method" do
get :edit, id: 1
assigns(:value).should eq "expected_value" # Set by concern_method
end
end
Now here's the weird part. I test using Guard. When Guard detects a save of the spec file and runs this test, it passes and Checkpoint!
is printed. Every time, completely deterministically. I can keep saving the file and it keeps passing. (If it helps, Guard uses Spring and runs RSpec.)
But when I run the test with an RSpec command, it fails. Weirdest of all, I would have expected a failure to occur because something weird happens with the anonymous controller setup and it can't find the route, but instead it's the should
that fails the test. When I run RSpec manually in this way, Checkpoint!
never prints, indicating that the anonymous controller action isn't getting called. But because I don't get a route error, some controller action must be getting called but I have no idea what, as I don't even have any other anonymous controllers with an edit
function that could possibly conflict. I've tried restarting Spring—no luck.
Any thoughts on what I could be doing wrong?
Edit:
Here's more info. The format of my concern is this:
module MyConcern
extend ActiveSupport::Concern
included do
puts "Included!"
private
def concern_method
puts "Method is called!"
"expected_value"
end
end
end
When the test fails (from me running RSpec explicitly with bundle exec rspec
), I get a pretty typical failure message:
Failure/Error: assigns(:value).should eq "expected_value"
expected: "expected_value"
got: nil
(compared using ==)
The top of my Guardfile is just guard :rspec, cmd: 'bundle exec spring rspec --color'
, and this behavior is the same even when I remove spring
from that command. I really have no idea how to debug this one.
I think that controller
receives an argument which will be used as a base class and if you don't pass one it will use the thing that you describe
. So your test is fishy :)
Try:
controller(ApplicationController) do
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With