Skip to content

Commit

Permalink
more controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlong committed Nov 24, 2009
1 parent 74a019c commit 5c41111
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 32 deletions.
80 changes: 64 additions & 16 deletions spec/acoustic/controller_spec.rb
@@ -1,38 +1,86 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'acoustic/controller'

class TestController < Acoustic::Controller
def show
@name = "world"
end
end

describe Acoustic::Controller do

before do
before :each do
@controller = Acoustic::Controller.new
@response = mock("response")
@request = MockRequest.new
@response = MockResponse.new
@controller.instance_variable_set('@_response', @response)
end

it 'should render text' do
@response.should_receive(:body=).with("Hello World!")
@controller.render(:text => "Hello World!")
end

it 'should render erb templates' do
@response.should_receive(:body=).with("Hello John!")
@controller.render(:template => template("hello.erb"))
describe '#render' do
it 'should render text' do
@response.should_receive(:body=).with("Hello World!")
@controller.render(:text => "Hello World!")
end

it 'should render erb templates' do
@response.should_receive(:body=).with("Hello John!")
render("hello.erb")
end

it 'should render instance variables from the controller to the erb template' do
@controller.instance_variable_set("@name", "John")
@response.should_receive(:body=).with("Hello John!")
render("hello_ivar.erb")
end

def render(filename)
@controller.render(:template => template(filename))
end
end

it 'should render instance variables from the controller to the erb template' do
@controller.instance_variable_set("@name", "John")
@response.should_receive(:body=).with("Hello John!")
@controller.render(:template => template("hello_ivar.erb"))
describe '#process' do
before :each do
@controller = TestController.new
@action = :show
@params = {}
end

it 'should set appropriate instance variables' do
process(:show)
@controller.instance_variable_get("@_params").should be(@params)
@controller.instance_variable_get("@_request").should be(@request)
@controller.instance_variable_get("@_response").should be(@response)
end

it 'should set the content type on the response' do
@response.should_receive(:[]=).with("Content-Type", "text/html")
process(:show)
end

it 'should call the appropriate action' do
@controller.should_receive(:show)
process(:show)

@controller.should_receive(:index)
process(:index)
end

it 'should render the correct template for the action'

def process(action = :show)
@controller.process(action, @params, @request, @response)
end
end

describe "Class Methods" do

it 'from_symbol should resolve a symbol to a controller' do
Acoustic::Controller.from_symbol(:test).should == TestController
end

it 'from_symbol should raise an error if the controller cannot be resolved' do
lambda { Acoustic::Controller.from_symbol(:bad) }.should raise_error(Acoustic::ControllerNameError, "Undefined controller BadController")
lambda {
Acoustic::Controller.from_symbol(:bad)
}.should raise_error(Acoustic::ControllerNameError, "Undefined controller BadController")
end
end

Expand Down
24 changes: 9 additions & 15 deletions spec/acoustic/dispatcher_spec.rb
Expand Up @@ -2,19 +2,10 @@
require 'acoustic/dispatcher'
require 'uri'

class TestRequest
def initialize(url)
@url = url
end
def request_uri
URI.parse(@url)
end
end

describe Acoustic::Dispatcher do

before do
@request = TestRequest.new("http://localhost/test/show")
@request = MockRequest.new
@response = Object.new
end

Expand All @@ -28,25 +19,28 @@ def request_uri
@router = Object.new
@params = {:controller => :test, :action => :show}
@router.should_receive(:resolve_uri).and_return(@params)
Acoustic::Controller.should_receive(:from_symbol).with(:test).and_return(TestController)
TestController.should_receive(:process).with(:show, @params, @request, @response)
Acoustic::Controller.should_receive(:from_symbol).with(:test).and_return(MockController)
MockController.should_receive(:process).with(:show, @params, @request, @response)
service(@request, @response)
end

it 'should rescue Acoustic::UnresolvableUriError and raise an Acoustic::NotFoundError' do
@router = Acoustic::Router.load(fixture_filename("dispatcher", "one_controller_urls.rb"))
@request = TestRequest.new("http://localhost/404")
@request = MockRequest.new("http://localhost/404")
lambda { service(@request, @response) }.should raise_error(Acoustic::NotFoundError, "Resource not found for http://localhost/404")
end

it 'should rescue Acoustic::ControllerNameError when the controller is not found and raise an Acoustic::NotFoundError' do
@request = TestRequest.new("http://localhost/bad")
lambda { service(@request, @response) }.should raise_error(Acoustic::NotFoundError, "Resource not found for http://localhost/bad")
@request = MockRequest.new("http://localhost/bad")
lambda {
service(@request, @response)
}.should raise_error(Acoustic::NotFoundError, "Resource not found for http://localhost/bad")
end

def service(request, response)
@router ||= Acoustic::Router.load(fixture_filename("dispatcher", "urls.rb"))
@dispatcher = Acoustic::Dispatcher.new(@router)
@dispatcher.service(request, response)
end

end
30 changes: 29 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -4,11 +4,39 @@

$LOAD_PATH << LIB_ROOT

class TestController
class MockController
def self.process(action, params, request, response)
end
end

class MockRequest
attr_accessor :uri

def initialize(uri = "http://localhost/test/show")
@uri = uri
end

def request_uri
URI.parse(@uri)
end
end

class MockResponse
attr_accessor :headers

def initialize
@headers = {}
end

def []=(key, value)
@headers[key] = value
end

def [](key)
@headers[key]
end
end

def fixture_filename(*path_parts)
File.expand_path(File.join(FIXTURES_ROOT, *path_parts))
end

0 comments on commit 5c41111

Please sign in to comment.