Testing Rails Metal/Rack with RSpec?
-
20-09-2019 - |
Question
Say I have a Metal class named Preview. How do I test it with RSpec?
When I try:
require 'spec_helper'
describe Preview do
it "should return the posted content" do
post "/preview", :content => "*title*"
response.body.should == "*title*"
end
end
I get:
undefined method `post' for #<ActiveSupport::TestCase::Subclass_1:0x1058b3098>
It seems that RSpec doesn't load up the :post
method if the test isn't explicitly for a Controller. I've tried specifying :type => :controller
to no avail.
Solution
rspec has integration specs that wrap rails integration tests (which go through rack). Just put that spec in ./spec/integration/preview_spec.rb
.
OTHER TIPS
I think it's better to test with rack/test
Nowadays (rspec-rails 2.2.0 and rails 3) you can achieve that using Request Specs:
According to the readme file:
Request specs live in spec/requests
[...]
Request specs mix in behavior from Rails' integration tests. See the docs for ActionDispatch::Integration::Runner for more information.