require 'helper' require 'focused_controller/test_helper' require 'action_controller' module FocusedController module TestHelper module FakePostsController class Action < ActionController::Base end class Index < Action def call if params[:omg] "omg" elsif params[:set_session] session[:foo] = 'omg' elsif params[:set_flash] flash[:foo] = 'omg' elsif params[:set_cookie] cookies[:foo] = 'omg' end end def self._routes OpenStruct.new( :named_routes => OpenStruct.new( :module => Module.new do def foo_path '/foo' end end ) ) end end class Show < Action def self._routes OpenStruct.new( :named_routes => OpenStruct.new( :module => Module.new do def bar_path '/bar' end end ) ) end end class TestCase < ActiveSupport::TestCase include FocusedController::TestHelper def initialize(method_name = :foo) super @_result = OpenStruct.new end def foo; end end class IndexTest < TestCase end class ShowTest < TestCase end class OtherShowTest < TestCase self.controller_class = Show end class OtherOtherShowTest < OtherShowTest end end describe TestHelper do it 'instantiates the correct controller' do mappings = { FakePostsController::IndexTest => FakePostsController::Index, FakePostsController::ShowTest => FakePostsController::Show, FakePostsController::OtherShowTest => FakePostsController::Show, FakePostsController::OtherOtherShowTest => FakePostsController::Show } mappings.each do |test, action| test.new.controller.is_a?(action).must_equal true end end let(:controller) { subject.controller } subject { FakePostsController::IndexTest.new } def must_fail(&block) block.must_raise ActiveSupport::TestCase::Assertion end def must_succeed(&block) block.call # this is just so the assertion is counted. the block will # raise if it fails assert_equal true, true end it 'supports assert_template :foo' do subject.controller.render :foo must_succeed { subject.assert_template :foo } must_fail { subject.assert_template :bar } end it "supports assert_template 'foo'" do subject.controller.render :foo must_succeed { subject.assert_template 'foo' } must_fail { subject.assert_template 'bar' } end it 'supports assert_response :success' do must_succeed { subject.assert_response :success } must_fail { subject.assert_response :error } subject.controller.render 'foo' must_succeed { subject.assert_response :success } must_fail { subject.assert_response :error } end it 'supports assert_response :redirect' do must_fail { subject.assert_response :redirect } subject.controller.redirect_to 'foo' must_succeed { subject.assert_response :redirect } end it 'supports assert_redirected_to' do must_fail { subject.assert_redirected_to '/foo' } subject.controller.redirect_to '/foo' must_succeed { subject.assert_redirected_to '/foo' } must_fail { subject.assert_redirected_to '/bar' } end it "responds to the controller's url helpers" do subject.respond_to?(:foo_path).must_equal true subject.respond_to?(:bar_path).must_equal false subject.foo_path.must_equal '/foo' other = FakePostsController::ShowTest.new other.respond_to?(:foo_path).must_equal false other.respond_to?(:bar_path).must_equal true other.bar_path.must_equal '/bar' end it 'supports session' do controller.params = { :set_session => true } controller.call subject.session[:foo].must_equal 'omg' subject.session['foo'].must_equal 'omg' end it 'supports flash' do controller.params = { :set_flash => true } controller.call subject.flash[:foo].must_equal 'omg' # This is consistent with the behaviour of standard rails functional tests subject.flash['foo'].must_equal nil end it 'supports cookies' do controller.params = { :set_cookie => true } controller.call subject.cookies[:foo].must_equal 'omg' end describe 'url stubbing' do before do subject.stub_url :foo, :bar end it 'handles equality' do foo, bar = Object.new, Object.new StubbedURL.new(:foo_path, [foo]).must_equal StubbedURL.new(:foo_path, [foo]) StubbedURL.new(:foo_path, [foo, bar]).wont_equal StubbedURL.new(:foo_path, [foo]) StubbedURL.new(:foo_path, [bar]).wont_equal StubbedURL.new(:foo_path, [foo]) StubbedURL.new(:bar_path, [foo]).wont_equal StubbedURL.new(:foo_path, [foo]) end it 'has a to_s' do StubbedURL.new(:foo_path, ['omg', 'lol']).to_s.must_equal "foo_path(omg, lol)" end it 'creates a stub method on the test and controller instances' do foo = Object.new subject.foo_path(foo).must_equal StubbedURL.new(:foo_path, [foo]) subject.controller.foo_path(foo).must_equal StubbedURL.new(:foo_path, [foo]) subject.foo_url(foo).must_equal StubbedURL.new(:foo_url, [foo]) subject.controller.foo_url(foo).must_equal StubbedURL.new(:foo_url, [foo]) end it 'works with a controller' do foo = Object.new subject.controller.redirect_to StubbedURL.new(:foo_path, [foo]) must_succeed { subject.assert_redirected_to StubbedURL.new(:foo_path, [foo]) } end end end end end