blowmage / minitest-rails-capybara

Capybara integration for MiniTest::Rails
http://blowmage.com/minitest-rails-capybara
MIT License
131 stars 40 forks source link

Adding Capybara RSpec Matchers to view specs #4

Closed sauy7 closed 11 years ago

sauy7 commented 12 years ago

As discussed in Google Group. See also https://github.com/blowmage/minitest-rails/pull/70

sauy7 commented 12 years ago

As per my comment on the linked pull request on the minitest-rails project, I'm now thinking that the RSpec matchers should be added to the View Helper and Controller tests.

To lib/minitest/rails/capybara.rb, I propose adding:

class MiniTest::Rails::ActionView::TestCase
  include Capybara::RSpecMatchers
end

class MiniTest::Rails::ActionController::TestCase
  include Capybara::RSpecMatchers

  def page
    response.body
  end
end

An example Helper and spec usage:

module DummiesHelper
  def hello
    content_tag :h1, "hello world"
  end
end
require "minitest_helper"

describe DummiesHelper do
  it "says hello" do
    render :text => "foo #{hello} bar"
    rendered.must_have_css "h1", :text => "hello world"
    rendered.must_have_content "foo hello world bar"
  end
end

An example spec for a controller and view generated in scaffolding:

it "must get index" do
    #setup
    @dummy.create

    # action
    get :index

    # standard Rails asserts
    assert_response :success
    assert_not_nil assigns(:dummies)
    assert_template :index
    assert_select "h1", "Listing dummies"

    # Capybara RSpec matchers 
    assert_have_content page, "Listing dummies"
    assert_have_link page, "Edit", href: edit_dummy_path(@dummy)
    assert_have_css page, "h1", text: "Listing dummies", count: 1

    page.must_have_content "Listing dummies"
    page.must_have_link "Edit", href: edit_dummy_path(@dummy)
    page.must_have_css "h1", text: "Listing dummies", count: 1
  end

I'll update the pull request if others agree.

blowmage commented 12 years ago

Makes sense. Do it!

SixArm commented 11 years ago

+1. Any progress on this? Thanks!

blowmage commented 11 years ago

Capybara is no longer being added to ActionController::TestCase or ActionView::TestCase. This project creates a new test class (Capybara::Rails::TestCase) for Capybara features. Closing this pull request as it is outdated.