Giter Club home page Giter Club logo

minitest-capybara's Introduction

minitest-capybara

Build Status

Capybara matchers support for Minitest unit & spec.

Why?

Capybara has good support for RSpec.

If you want to use it with Minitest, you can of course write:

assert page.has_content?("Hello")

but:

  1. it's kinda ugly
  2. you don't have meaningfull error messages.

With this project Minitest gets all the good stuff.

Rails integration

minitest-capybara (and capybara) works with Rails out of the box, remember to require capybara/rails.

See example Rails app: https://github.com/wojtekmach/minitest-capybara-example.

For more features, check out: minitest-rails-capybara.

Usage

With minitest/test:

class HomeTest < Minitest::Capybara::Test
  def test_home
    visit "/"

    assert_content "Homepage"

    within ".login" do
      refute_content "Signed in as"
    end

    assert_link "Sign in"
    assert_link find(".login"), "Sign in"

    assert_selector 'li:first', text: "Item 1"
  end
end

With minitest/spec:

class HomeSpec < Minitest::Capybara::Spec
  it "works" do
    visit "/"

    page.must_have_content "Homepage"

    within ".login" do
      page.wont_have_content "Signed in as"
    end

    find(".login").must_have_link("Sign in")

    page.must_have_selector 'li:first', text: "Item 1"
  end
end

Instead of inheriting directly from Minitest::Capybara::Test (or Spec) it's usually better to create a custom test base class:

# test/acceptance_test_helper.rb
require "minitest/autorun"

class AcceptanceTest < Minitest::Capybara::Test
  # custom methods, before blocks etc.
end

If you need to inherit from a different base class (e.g. ActiveSupport::TestCase) you can do this instead:

# test/acceptance_test_helper.rb
require "test_helper"

class AcceptanceTest < ActiveSupport::TestCase
  include Minitest::Capybara::Behaviour

  # custom methods, before blocks etc.
end

Capybara drivers

Switching drivers is easy with minitest-metadata:

require 'minitest-metadata'

class AcceptanceSpec < Minitest::Capybara::Spec
  before do
    if metadata[:js]
      Capybara.current_driver = Capybara.javascript_driver
    else
      Capybara.current_driver = Capybara.default_driver
    end
  end
end

class HomeSpec < AcceptanceSpec
  it "home with ajax", js: true do
    visit "/"
    page.must_have_content "AJAX enabled..."
  end
end

License

minitest-capybara is released under the MIT License.

minitest-capybara's People

Contributors

acnalesso avatar apotonick avatar blowmage avatar jonatack avatar joseramonc avatar patru avatar wojtekmach avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.