Code Monkey home page Code Monkey logo

shoulda's Introduction

shoulda

The shoulda gem is a meta gem with two dependencies:

Official documentation for each gem:

The following describes different use cases and combinations.

rspec with shoulda-matchers

This is what thoughtbot currently does. We write tests like:

describe Post do
  it { should belong_to(:user) }
  it { should validate_presence_of(:title) }
end

The belong_to and validate_presence_of methods are the matchers. All matchers are Rails 3-specific.

Add rspec-rails and shoulda-matchers to the project's Gemfile:

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

test/unit with shoulda

For the folks who prefer Test::Unit, they'd write tests like:

class UserTest < Test::Unit::TestCase
  should have_many(:posts)
  should_not allow_value("blah").for(:email)
end

The have_many and allow_value methods are the same kind of matchers seen in the RSpec example. They come from the shoulda-matchers gem.

Add shoulda to the project's Gemfile:

group :test do
  gem 'shoulda'
end

test/unit with shoulda-context

If you're not testing a Rails project or don't want to use the matchers, you can use shoulda-context independently to write tests like:

class CalculatorTest < Test::Unit::TestCase
  context "a calculator" do
    setup do
      @calculator = Calculator.new
    end

    should "add two numbers for the sum" do
      assert_equal 4, @calculator.sum(2, 2)
    end

    should "multiply two numbers for the product" do
      assert_equal 10, @calculator.product(2, 5)
    end
  end
end

Add shoulda-context to the project's Gemfile:

group :test do
  gem 'shoulda-context'
end

Credits

thoughtbot

Shoulda is maintained and funded by thoughtbot, inc

Thank you to all the contributors!

The names and logos for thoughtbot are trademarks of thoughtbot, inc.

License

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

shoulda's People

Contributors

augustl avatar bjhess avatar boone avatar cristibalan avatar drmark avatar gfairbrother avatar infused avatar jferris avatar joshuaclayton avatar kron4eg avatar lawrencepit avatar lazyatom avatar lmarburger avatar metaskills avatar mike-burns avatar mjankowski avatar mlins avatar packetmonkey avatar pragdave avatar qrush avatar rmm5t avatar showaltb avatar snowblink avatar stevenbristol avatar technicalpickles avatar terceiro avatar thechrisoshow avatar tomlea avatar webmat avatar zenspider avatar

Stargazers

 avatar

Watchers

 avatar  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.