I'm trying to use Capybara + Rspec to test a Sinatra app, and I'm having some trouble integrating Capybara with Rspec. Here's my spec_helper.rb
:
require './myapp'
require 'capybara/rspec'
Capybara.app = MyApp
Then, in my app_spec.rb
, I have:
require_relative 'spec_helper'
describe "My App", type: :request do
describe "get 'home'" do
it 开发者_运维问答"should be successful" do
visit '/'
response.should be_successful
end
end
end
According to all the documentation I can find, this should work fine . . . but when I run rspec spec
, I get this:
Failures:
1) Our App get 'home' should be successful
Failure/Error: response.should be_successful
NameError:
undefined local variable or method `response' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f8321289e60>
# ./spec/app_spec.rb:11:in `block (3 levels) in <top (required)>'
I've done a bit of digging around, and I can solve the problem by throwing this method into the spec file:
def response
page.driver.browser.last_response
end
But that feels hacky.
Is there a better solution? What am I missing?
Well there is no response method. I think that's only in the Rails addons for RSpec2/Capybara (not sure about this).
Just use last_response
instead of response
:
last_response.should be_successful
and you should be fine.
Edit:
Seems like you are missing some test methods. Try this in your spec_helper.rb
:
RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
You will need to require rack/test
before.
精彩评论