<commit_before>Feature: view spec

  View specs are marked by `:type => :view`
  or if you have set `config.infer_spec_type_from_file_location!`
  by placing them in `spec/views`.

  Use them to test the content of view templates
  without invoking a specific controller.
  They generally follow three steps:

  ```ruby
  assign(:widget, Widget.new)  # sets @widget = Widget.new in the view template

  render

  expect(rendered).to match(/text/)
  ```

  1. Use the `assign` method to set instance variables in the view.

  2. Use the `render` method to render the view.

  3. Set expectations against the resulting rendered template.

  Scenario: View specs render the described view file
    Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "widgets/index" do
        it "displays all the widgets" do
          assign(:widgets, [
            Widget.create!(:name => "slicer"),
            Widget.create!(:name => "dicer")
          ])

          render

          expect(rendered).to match /slicer/
          expect(rendered).to match /dicer/
        end
      end
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can have before block and nesting
    Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "widgets/index" do

        context "with 2 widgets" do
          before(:each) do
            assign(:widgets, [
              Widget.create!(:name => "slicer"),
              Widget.create!(:name => "dicer")
            ])
          end

          it "displays both widgets" do
            render

            expect(rendered).to match /slicer/
            expect(rendered).to match /dicer/
          end
        end
      end
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can explicitly render templates
    Given a file named "spec/views/widgets/widget.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "rendering the widget template" do
        it "displays the widget" do
          assign(:widget, Widget.create!(:name => "slicer"))

          render :template => "widgets/widget.html.erb"

          expect(rendered).to match /slicer/
        end
      end
      """
    And a file named "app/views/widgets/widget.html.erb" with:
      """
      <h2><%= @widget.name %></h2>
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can render templates in layouts
    Given a file named "spec/views/widgets/widget.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "rendering the widget template" do
        context "with the inventory layout" do
          it "displays the widget" do
            assign(:widget, Widget.create!(:name => "slicer"))

            render :template => "widgets/widget.html.erb", :layout => "layouts/inventory"

            expect(rendered).to match /slicer/
          end
        end
      end
      """
    And a file named "app/views/widgets/widget.html.erb" with:
      """
      <h2><%= @widget.name %></h2>
      """
    And a file named "app/views/layouts/inventory.html.erb" with:
      """
      <%= yield %>
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can have description that includes the format and handler
    Given a file named "spec/views/widgets/widget.xml.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "widgets/widget.html.erb" do
        it "renders the HTML template" do
          render

          expect(rendered).to match /HTML/
        end
      end

      RSpec.describe "widgets/widget.xml.erb" do
        it "renders the XML template" do
          render

          expect(rendered).to match /XML/
        end
      end
      """
    And a file named "app/views/widgets/widget.html.erb" with:
      """
      HTML
      """
    And a file named "app/views/widgets/widget.xml.erb" with:
      """
      XML
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can render locals in a partial
    Given a file named "spec/views/widgets/_widget.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "rendering locals in a partial" do
        it "displays the widget" do
          widget = Widget.create!(:name => "slicer")

          render :partial => "widgets/widget.html.erb", :locals => {:widget => widget}

          expect(rendered).to match /slicer/
        end
      end
      """
    And a file named "app/views/widgets/_widget.html.erb" with:
      """
      <h3><%= widget.name %></h3>
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can render locals in an implicit partial<commit_msg>Passing formats or handlers to render :template and friends is deprecated<commit_after>Feature: view spec

  View specs are marked by `:type => :view`
  or if you have set `config.infer_spec_type_from_file_location!`
  by placing them in `spec/views`.

  Use them to test the content of view templates
  without invoking a specific controller.
  They generally follow three steps:

  ```ruby
  assign(:widget, Widget.new)  # sets @widget = Widget.new in the view template

  render

  expect(rendered).to match(/text/)
  ```

  1. Use the `assign` method to set instance variables in the view.

  2. Use the `render` method to render the view.

  3. Set expectations against the resulting rendered template.

  Scenario: View specs render the described view file
    Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "widgets/index" do
        it "displays all the widgets" do
          assign(:widgets, [
            Widget.create!(:name => "slicer"),
            Widget.create!(:name => "dicer")
          ])

          render

          expect(rendered).to match /slicer/
          expect(rendered).to match /dicer/
        end
      end
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can have before block and nesting
    Given a file named "spec/views/widgets/index.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "widgets/index" do

        context "with 2 widgets" do
          before(:each) do
            assign(:widgets, [
              Widget.create!(:name => "slicer"),
              Widget.create!(:name => "dicer")
            ])
          end

          it "displays both widgets" do
            render

            expect(rendered).to match /slicer/
            expect(rendered).to match /dicer/
          end
        end
      end
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can explicitly render templates
    Given a file named "spec/views/widgets/widget.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "rendering the widget template" do
        it "displays the widget" do
          assign(:widget, Widget.create!(:name => "slicer"))

          render :template => "widgets/widget"

          expect(rendered).to match /slicer/
        end
      end
      """
    And a file named "app/views/widgets/widget.html.erb" with:
      """
      <h2><%= @widget.name %></h2>
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can render templates in layouts
    Given a file named "spec/views/widgets/widget.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "rendering the widget template" do
        context "with the inventory layout" do
          it "displays the widget" do
            assign(:widget, Widget.create!(:name => "slicer"))

            render :template => "widgets/widget", :layout => "layouts/inventory"

            expect(rendered).to match /slicer/
          end
        end
      end
      """
    And a file named "app/views/widgets/widget.html.erb" with:
      """
      <h2><%= @widget.name %></h2>
      """
    And a file named "app/views/layouts/inventory.html.erb" with:
      """
      <%= yield %>
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can have description that includes the format and handler
    Given a file named "spec/views/widgets/widget.xml.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "widgets/widget.html.erb" do
        it "renders the HTML template" do
          render

          expect(rendered).to match /HTML/
        end
      end

      RSpec.describe "widgets/widget.xml.erb" do
        it "renders the XML template" do
          render

          expect(rendered).to match /XML/
        end
      end
      """
    And a file named "app/views/widgets/widget.html.erb" with:
      """
      HTML
      """
    And a file named "app/views/widgets/widget.xml.erb" with:
      """
      XML
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can render locals in a partial
    Given a file named "spec/views/widgets/_widget.html.erb_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "rendering locals in a partial" do
        it "displays the widget" do
          widget = Widget.create!(:name => "slicer")

          render :partial => "widgets/widget", :locals => {:widget => widget}

          expect(rendered).to match /slicer/
        end
      end
      """
    And a file named "app/views/widgets/_widget.html.erb" with:
      """
      <h3><%= widget.name %></h3>
      """
    When I run `rspec spec/views`
    Then the examples should all pass

  Scenario: View specs can render locals in an implicit partial
{'fraction_non_alphanumeric': 0.10358526169430926, 'fraction_numerical': 0.001986294567484358, 'mean_word_length': 2.1166821417517796, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7232911', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2871, 'n_words': 961}