Giter Club home page Giter Club logo

minitest-rails-shoulda's Introduction

minitest-rails-shoulda

Description

Make shoulda-matchers available for minitest-rails.

Installation

In Rails 3 and Bundler, add the following to your Gemfile:

group :test do
  gem "minitest-rails-shoulda"
end

Add the following to your test helper:

require "minitest/rails/shoulda"

Usage

ActiveRecord Matchers

Matchers to test associations:

describe Post do
  subject { Post.new }
  it { must belong_to(:user) }
  it { must have_many(:tags).through(:taggings) }
end

describe User do
  subject { User.new }
  it { must have_many(:posts) }
end

ActiveModel Matchers

Matchers to test validations and mass assignments:

describe Post do
  subject { Post.new }
  it { must validate_uniqueness_of(:title) }
  it { must validate_presence_of(:body).with_message(/wtf/) }
  it { must validate_presence_of(:title) }
  it { must validate_numericality_of(:user_id) }
end

describe User do
  subject { User.new }
  it { wont allow_value("blah").for(:email) }
  it { must allow_value("[email protected]").for(:email) }
  it { must ensure_inclusion_of(:age).in_range(1..100) }
  it { wont allow_mass_assignment_of(:password) }
end

ActionController Matchers

Matchers to test common patterns:

describe PostsController, "#show" do
  context "for a fictional user" do
    before do
      get :show, :id => 1
    end

    it { must assign_to(:user) }
    it { must respond_with(:success) }
    it { must render_template(:show) }
    it { wont set_the_flash }
  end
end

Shoulda Assertions (Expectations)

Assertions:

# collection contains some value
assert_contains collection, "a"
assert_contains collection, /a/
assert_contains collection, 3

# or

refute_does_not_contain collection, "a"
refute_does_not_contain collection, /a/
refute_does_not_contain collection, 3


# collection does not contain some value
refute_contains collection, "b"
refute_contains collection, /b/
refute_contains collection, 5

# or

assert_does_not_contain collection, "b"
assert_does_not_contain collection, /b/
assert_does_not_contain collection, 5

# collections contain the same elements
assert_same_elements [1, b, "3"], ["3", 1, b]

# collection does not contain the same elements
refute_same_elements [1, b, "3"], ["4", b, 1]

And as Expectations:

# collection contains some value
collection.must_contain "a"
collection.must_contain /a/
collection.must_contain 3

# collection does not contain some value
collection.wont_contain "b"
collection.wont_contain /b/
collection.wont_contain 5

# collections contain the same elements
[1, b, "3"].must_have_same_elements ["3", 1, b]

# collection does not contain the same elements
[1, b, "3"].wont_have_same_elements ["4", b, 1]

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Contributors

Many thanks go to the following who have contributed to making this library even better:

License

minitest-rails-shoulda

  • Freely distributable and licensed under the MIT license.

minitest-rails-shoulda's People

Contributors

bousquet avatar phlipper avatar blowmage avatar sprysoft avatar elskwid avatar

Watchers

Phil Crissman avatar James Cloos 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.