Adding Capybara RSpec Matchers to view specs #4

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
3 participants

sauy7 commented Aug 27, 2012

As discussed in Google Group. See also blowmage/minitest-rails#70

@blowmage blowmage commented on an outdated diff Aug 27, 2012

test/test_matchers_in_view_tests.rb
@@ -0,0 +1,153 @@
+require "rails_helper"
+
+describe "Capybara Matchers View Test" do
+
+ CONTENT = "<!DOCTYPE html><title>TestApp</title>
+ <h1>Hello <span>World</span></h1>
+ <nav><ul><li><a href='/'>home</a></li></ul></nav>
+ <p><label>Email Address<input type='text'></label></p>
+ <button>random button</button>
+ <label>going<input type='checkbox' checked='checked'></label>
+ <label>avoid<input type='checkbox'></label>"
+
+ describe "have_content" do
+ it "rendered with content" do
+ render inline: CONTENT
@blowmage

blowmage Aug 27, 2012

Owner

This is using 1.9's hash syntax. Rails 3.x supports Ruby 1.8.7, and therefore so should we. Please update.

blowmage was assigned Aug 27, 2012

sauy7 commented Aug 29, 2012

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.

Owner

blowmage commented Aug 31, 2012

Makes sense. Do it!

sauy7 referenced this pull request in blowmage/minitest-rails Sep 18, 2012

Closed

Adding view specs #70

@ghost

ghost commented Dec 2, 2012

+1. Any progress on this? Thanks!

Owner

blowmage commented Jul 23, 2013

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.

blowmage closed this Jul 23, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment