Skip to content
This repository
tree: 5f869c1b81
Fetching contributors…

Cannot retrieve contributors at this time

file 220 lines (192 sloc) 6.44 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
require "spec_helper"

module RSpec::Rails
  describe ViewExampleGroup do
    it { should be_included_in_files_in('./spec/views/') }
    it { should be_included_in_files_in('.\\spec\\views\\') }

    it "adds :type => :view to the metadata" do
      group = RSpec::Core::ExampleGroup.describe do
        include ViewExampleGroup
      end
      group.metadata[:type].should eq(:view)
    end

    describe 'automatic inclusion of helpers' do
      module ::ThingsHelper; end
      module ::Namespaced; module ThingsHelper; end; end

      it 'includes the helper with the same name' do
        group = RSpec::Core::ExampleGroup.describe 'things/show.html.erb'
        group.should_receive(:helper).with(ThingsHelper)
        group.class_eval do
          include ViewExampleGroup
        end
      end

      it 'includes the namespaced helper with the same name' do
        group = RSpec::Core::ExampleGroup.describe 'namespaced/things/show.html.erb'
        group.should_receive(:helper).with(Namespaced::ThingsHelper)
        group.class_eval do
          include ViewExampleGroup
        end
      end

      it 'operates normally when no helper with the same name exists' do
        raise 'unexpected constant found' if Object.const_defined?('ClocksHelper')
        lambda {
          RSpec::Core::ExampleGroup.describe 'clocks/show.html.erb' do
            include ViewExampleGroup
          end
        }.should_not raise_error
      end

      context 'application helper exists' do
        before do
          if !Object.const_defined? 'ApplicationHelper'
            module ::ApplicationHelper; end
            @application_helper_defined = true
          end
        end

        after do
          if @application_helper_defined
            Object.__send__ :remove_const, 'ApplicationHelper'
          end
        end

        it 'includes the application helper' do
          group = RSpec::Core::Example.describe 'bars/new.html.erb'
          group.should_receive(:helper).with(ApplicationHelper)
          group.class_eval do
            include ViewExampleGroup
          end
        end
      end

      context 'no application helper exists' do
        before do
          if Object.const_defined? 'ApplicationHelper'
            @application_helper = ApplicationHelper
            Object.__send__ :remove_const, 'ApplicationHelper'
          end
        end

        after do
          if @application_helper
            ApplicationHelper = @application_helper
          end
        end

        it 'operates normally' do
          lambda {
            RSpec::Core::ExampleGroup.describe 'foos/edit.html.erb' do
              include ViewExampleGroup
            end
          }.should_not raise_error
        end
      end
    end

    describe "#render" do
      let(:view_spec) do
        Class.new do
          module Local
            def received
              @received ||= []
            end
            def render(options={}, local_assigns={}, &block)
              received << [options, local_assigns, block]
            end
            def _assigns
              {}
            end
          end
          include Local
          include ViewExampleGroup::ExampleMethods
        end.new
      end

      context "given no input" do
        it "sends render(:template => (described file)) to the view" do
          view_spec.stub(:_default_file_to_render) { "widgets/new" }
          view_spec.render
          view_spec.received.first.should == [{:template => "widgets/new"},{}, nil]
        end

        it "converts the filename components into render options" do
          view_spec.stub(:_default_file_to_render) { "widgets/new.en.html.erb" }
          view_spec.render

          if ::Rails.version >= "3.2"
            view_spec.received.first.should == [{:template => "widgets/new", :locales=>['en'], :formats=>['html'], :handlers=>['erb']}, {}, nil]
          else
            view_spec.received.first.should == [{:template => "widgets/new.en.html.erb"}, {}, nil]
          end
        end
      end

      context "given a string" do
        it "sends string as the first arg to render" do
          view_spec.render('arbitrary/path')
          view_spec.received.first.should == ["arbitrary/path", {}, nil]
        end
      end

      context "given a hash" do
        it "sends the hash as the first arg to render" do
          view_spec.render(:foo => 'bar')
          view_spec.received.first.should == [{:foo => "bar"}, {}, nil]
        end
      end
    end

    describe '#params' do
      let(:view_spec) do
        Class.new do
          include ViewExampleGroup::ExampleMethods
          def controller
            @controller ||= Object.new
          end
        end.new
      end

      it 'delegates to the controller' do
        view_spec.controller.should_receive(:params).and_return({})
        view_spec.params[:foo] = 1
      end
    end

    describe "#_controller_path" do
      let(:view_spec) do
        Class.new do
          include ViewExampleGroup::ExampleMethods
        end.new
      end
      context "with a common _default_file_to_render" do
        it "it returns the directory" do
          view_spec.stub(:_default_file_to_render).
            and_return("things/new.html.erb")
          view_spec.__send__(:_controller_path).
            should == "things"
        end
      end

      context "with a nested _default_file_to_render" do
        it "it returns the directory path" do
          view_spec.stub(:_default_file_to_render).
            and_return("admin/things/new.html.erb")
          view_spec.__send__(:_controller_path).
            should == "admin/things"
        end
      end
    end

    describe "#view" do
      let(:view_spec) do
        Class.new do
          include ViewExampleGroup::ExampleMethods
        end.new
      end

      it "delegates to _view" do
        view = double("view")
        view_spec.stub(:_view) { view }
        view_spec.view.should == view
      end
    end

    describe "#template" do
      let(:view_spec) do
        Class.new do
          include ViewExampleGroup::ExampleMethods
          def _view; end
        end.new
      end

      before { RSpec.stub(:deprecate) }

      it "is deprecated" do
        RSpec.should_receive(:deprecate)
        view_spec.template
      end

      it "delegates to #view" do
        view_spec.should_receive(:view)
        view_spec.template
      end
    end
  end
end
Something went wrong with that request. Please try again.