Git Product home page Git Product logo

shoulda-matchers's Introduction

shoulda-matchers Build Status

Official Documentation

Test::Unit- and RSpec-compatible one-liners that test common Rails functionality. These tests would otherwise be much longer, more complex, and error-prone.

Refer to the shoulda-context gem if you want to know more about using shoulda with Test::Unit.

ActiveRecord Matchers

Matchers to test associations:

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

describe User do
  it { should have_many(:posts) }
end

ActiveModel Matchers

Matchers to test validations and mass assignments:

describe Post do
  it { should validate_uniqueness_of(:title) }
  it { should validate_presence_of(:body).with_message(/wtf/) }
  it { should validate_presence_of(:title) }
  it { should validate_numericality_of(:user_id) }

  # validates_uniqueness_of requires an entry to be in the database already
  it "validates uniqueness of title" do
    Post.create!(title: "My Awesome Post", body: "whatever")
    should validate_uniqueness_of(:title)
  end
end

describe User do
  it { should_not allow_value("blah").for(:email) }
  it { should allow_value("[email protected]").for(:email) }
  it { should ensure_inclusion_of(:age).in_range(1..100) }
  it { should_not 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 { should assign_to(:user) }
    it { should respond_with(:success) }
    it { should render_template(:show) }
    it { should_not set_the_flash }
  end
end

Installation

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

group :test do
  gem "rspec-rails"
  gem "shoulda-matchers"
end

Shoulda will automatically include matchers into the appropriate example groups.

Credits

Shoulda is maintained and funded by thoughtbot. Thank you to all the contributors.

License

Shoulda is Copyright © 2006-2010 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

shoulda-matchers's People

Contributors

jferris avatar rmm5t avatar mike-burns avatar sikachu avatar mjankowski avatar joshuaclayton avatar thechrisoshow avatar mdaubs avatar technicalpickles avatar webmat avatar boone avatar vjt avatar wingrunr21 avatar sweed avatar qrush avatar lawrencepit avatar jasonm avatar hakon avatar showaltb avatar danieltamiosso avatar sigi avatar bloy avatar tmilewski avatar tristandunn avatar shaliko avatar stevenbristol avatar morgoth avatar nashby avatar tomlea avatar kollektiv avatar

Stargazers

 avatar

Watchers

 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.