require 'test_helper'
require 'roar/rails/test_case'
require "dummy/config/environment"
require "rails/test_help" # adds stuff like @routes, etc.
module Representer
module BMP
class Album; end
end
end
class ControllerMethodsTest < ActionController::TestCase
tests AlbumsController
test "responds to #responder" do
assert_equal Roar::Rails::ControllerMethods::Responder, @controller.class.responder
end
test "responds to #represents" do
@controller = Class.new(AlbumsController)
assert_equal Album, @controller.represented_class
@controller.represents Song
assert_equal Song, @controller.represented_class
end
test "responds to #representer_class_for" do
assert_equal Representer::BMP::Album, @controller.representer_class_for(Album, :bmp)
end
test "responds to #representation" do
post :create, %{2011Walking In Your Footsteps}, :format => :xml
assert_equal({"id"=>"", "year"=>"2011",
"songs_attributes"=>[{"title"=>"Walking In Your Footsteps"}]}, @controller.representation)
end
test "responds to #incoming" do
post :create, %{2011Walking In Your Footsteps}, :format => :xml
assert_equal({"id"=>"", "year"=>"2011",
"songs"=>[{"title"=>"Walking In Your Footsteps"}], "links"=>[]}, @controller.incoming.to_attributes)
end
end
class ControllerFunctionalTest < ActionController::TestCase
tests AlbumsController
test "GET: returns a xml representation" do
get :show, :id => 1, :format => :xml
assert_response 200
assert_body %{
12011AlltaxBali}, :format => :xml
end
test "POST: creates a new album and returns the xml representation" do
post :create, %{1997Cooler Than You}, :format => :xml
assert @album = Album.find(:last)
assert_equal "1997", @album.year
assert_equal "Cooler Than You", @album.songs.first.title
assert_response 201, "Location" => album_url(@album) # Created
assert_body %{
21997Cooler Than You}, :format => :xml
end
test "POST: invalid incoming representations yields to 422" do
post :create, :format => :xml
assert_response 422 # Unprocessable Entity
end
test "PUT: updates album and returns the xml representation" do
put :update, %{
1997Cooler Than YouRubbing The Elf}, :id => 1, :format => :xml
assert @album = Album.find(1)
assert_equal "1997", @album.year
assert_equal 2, @album.songs.size
assert_equal "Cooler Than You", @album.songs.first.title
assert_equal "Rubbing The Elf", @album.songs.last.title
assert_response 200
assert_body %{
11997Cooler Than YouRubbing The Elf}, :format => :xml
end
end