Serves controller action responses under several conditions.
Alfred creates fixture files of your controller responses so you can use them in your tests. Ideal if your app's client is build with a javascript framework and you want to test responses under several conditions.
Add the gem to your Gemfile (inside test group).
gem 'alfred_rails', :require => 'alfred'
Install bundle by running:
$ bundle install
Create the Alfred configuration helper by running:
$ rails g alfred:install
You can create empty definitions by running:
$ rails g alfred:controller api/v1/posts
Here's an example of a definition:
# spec/alfreds/api/v1/posts_controller.rb
Alfred.define do
setup do
sign_in :user, create(:user)
end
controller Api::V1::PostsController do
scenario 'update post by manager' do
setup do
create(:post, :title => 'Alfred is awesome', :body => 'It saves me time')
end
patch :update, {
:format => :json,
:id => 1,
:post => {
:title => 'Alfred rocks!'
}
}
end
end
end
This will create a fixture file which you can use in your javascript tests at:
spec/javascripts/fixtures/api/v1/posts/update/update_by_manager.js
There are just a few configuration options listed below:
include
The modules you want to include in ActionController::TestCase.setup
Runs before every scenario.mock_with
Mocking framework of your choise.fixture_path
Where to save the fixtures.
See example below:
# spec/alfred_helper.rb
Alfred.configure do |config|
## Includes
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers
## Setup
config.setup do
Apartment::Database.stub(:create).and_return(true)
end
## Mocking framework
config.mock_with :rspec
config.mock_with :mocha
config.mock_with :rr
config.mock_with :flexmock
## Fixture path
config.fixture_path 'spec/javascripts/fixtures'
end
After defining and generating Alfred fixtures they are accessible in your JavaScript tests.
# Get request response
Alfred.serve('posts_controller/update', 'update post by manager')
# Example of a test
describe 'PostModel', ->
describe '#update', ->
it 'should update model', ->
response = Alfred.serve('posts_controller/update', 'update post by manager')
@server = sinon.fakeServer.create()
@server.respondWith 'PATCH', 'posts/1', [200, { 'Content-Type': 'application/json' }, response]
@post.update()
@server.respond()
@post.updated().should.equal(true)
Implementation on this differs on which libraries you are using to test with. In the above example we're using SinonJS to create a fake server response.
# Creates fake server and calls respondWith
Alfred.SinonAdapter.serve('posts_controller/update', 'update post by manager')
# Example of a test
describe 'PostModel', ->
describe '#update', ->
it 'should update model', ->
@server = Alfred.SinonAdapter.serve('posts_controller/update', 'update post by manager')
@post.update()
@server.respond()
@post.updated().should.equal(true)
By calling Alfred.fetch
you can fetch a scenario object with meta data, such as path, request method etc. This can be useful when stubbing a request;
Alfred.fetch('posts/update', 'update post by manager') # => Object
Add the gem to your Gemfile (inside development group):
gem 'guard-alfred', :require => false
Add guard definition to your Guardfile by running this command:
$ guard init alfred
Make sure to put this block on top of your Guardfile so all fixtures are created before running tests.
guard :alfred do
watch(%r{^app/controllers/(.+)\.rb$}) { |m| "spec/alfreds/#{m[1]}.rb" }
watch(%r{^spec/alfreds/(.+)\.rb$}) { |m| "spec/alfreds/#{m[1]}.rb" }
end
Please read Guard usage doc for usage instructions.
This library is tested against Travis and aims to support the following Ruby implementations:
- Ruby 1.9.3
- Ruby 2.0.0
- Ruby 2.1.1
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Copyright (c) 2014 Johan van Zonneveld. See LICENSE for details.