开发者_开发问答In RSpec I'm currently testing API requests using a series of methods that compile effectively to something like this:
env = Rack::MockRequest.env_for('/api/v1/projects.json')
Rails.application.call(env)
I was wondering if there was a shorter way of making a request to the Rails application in RSpec. I know you can create a controller spec and request specific actions, but I'm more after a way to make a request to a URL and check that the output is what I expect.
I like using Rack::Test for this use case. Example:
require 'spec_helper'
require 'rack/test'
describe 'API' do
include Rack::Test::Methods
def app
Rails.application
end
before do
@password = "12345"
@project = Factory(:project)
@user = Factory(:user,
:password => @password,
:password_confirmation => @password,
:project => @project)
authorize @user.email, @password
end
it "gets projects" do
get "/api/v1/projects.json"
last_response.should be_ok
last_response.body.should == [@project].to_json
last_response.content_type.should == 'application/json'
end
end
The "visit" method seems to work too, but i dont know about the mocking.
精彩评论