Questions tagged [shoulda]

Shoulda, developed by thoughtbot, provides constructs for organizing Test::Unit tests and matchers for testing Ruby on Rails applications that work with Test::Unit or RSpec.

Shoulda organizes Test::Unit tests into a collection of nested contexts with support for setup and teardown blocks and should clauses. Shoulda also provides matchers for testing Ruby on Rails applications that work with Test::Unit, Minitest or RSpec.

Along with shoulda, thoughtbot provides many many other community libraries.


Resources


Related tags

383 questions
133
votes
10 answers

How to run Rails console in the test environment and load test_helper.rb?

The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For…
Ethan
  • 57,819
  • 63
  • 187
  • 237
71
votes
1 answer

Shoulda/RSpec matchers - conditional validation

In my code I had the following validation with Shoulda matchers, which works fine: it { should validate_presence_of(:name) } In my model, I've added the condition to my validation: validates_presence_of :name, :if => eligible? Is it possible to…
alexs333
  • 12,143
  • 11
  • 51
  • 89
45
votes
4 answers

How can I test :inclusion validation in Rails using RSpec

I have the following validation in my ActiveRecord. validates :active, :inclusion => {:in => ['Y', 'N']} I am using the following to test my model validations. should_not allow_value('A').for(:active) should allow_value('Y').for(:active) should…
jake
  • 2,371
  • 1
  • 20
  • 31
33
votes
8 answers

Why should I use RSpec or shoulda with Rails?

I am setting up a rails app and I just finished making some unit tests and my friend said that apparently fixtures are no longer cool and people are now using RSpec or shoulda. I was wondering what the actual benefits are to use these other…
Frew Schmidt
  • 9,364
  • 16
  • 64
  • 86
27
votes
3 answers

How can I use `should validate_presence_of` with a custom error message?

I'm testing my ActiveRecord models with Rspec. I've just added a custom error message to one of my validations, like this: validates :accepted_terms_at, :presence => {:message => 'You must accept the Terms and Conditions to use this site.'} Now the…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
27
votes
2 answers

shoulda-matchers RSpec expect syntax

What is the correct format for using shoulda-matchers and RSpec's new expect syntax?
trev9065
  • 3,371
  • 4
  • 30
  • 45
26
votes
2 answers

Can't get uniqueness validation test pass with shoulda matcher

I have a shoulda matcher in my avatar_parts_spec.rb and I can't get it pass: Test: require 'rails_helper' RSpec.describe AvatarPart, :type => :model do it { should validate_presence_of(:name) } it { should validate_presence_of(:type) } it {…
Barna Kovacs
  • 1,226
  • 1
  • 14
  • 35
26
votes
1 answer

when does factory girl create objects in db?

I am trying to simulate a session using FactoryGirl/shoulda (it worked with fixtures but i am having problems with using factories). I have following factories (user login and email both have unique validations): Factory.define :user do |u| …
Pavel K.
  • 6,697
  • 8
  • 49
  • 80
24
votes
5 answers

Shoulda belongs_to with class_name and foreign_key

I know you can easily test a belongs to relationship using Shoulda: describe Dog dog it { should belong_to(:owner) } end Is it possible to test a more complicated belongs_to relationship using Shoulda? Something like this: class Dog <…
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
23
votes
2 answers

How do you specify POST params in a Rails test?

Working with Test::Unit and Shoulda. Trying to test Users.create. My understanding is that Rails forms send params for an object like this: user[email] Which turns into hash in your action, right? params[:user][:email] OK, so in my test I've…
Ethan
  • 57,819
  • 63
  • 187
  • 237
21
votes
2 answers

Rails: Shoulda-matchers belong_to optional test

I have a model class Certificate < ApplicationRecord notification_object belongs_to :user belongs_to :share_class, optional: true belongs_to :round, optional: true belongs_to :company, optional: true has_many :approvals, as:…
meerkat
  • 361
  • 1
  • 3
  • 11
21
votes
3 answers

How to run a single shoulda context test in ruby Test::Unit framework

I can typically test a regular Test::Unit method using the following commandline syntax for a method "delete_user_test": ruby functional/user_controller_test.rb -n delete_user_test Now when I'm using the shoulda plugin with Test::Unit I try to use…
Joe Dean
  • 3,356
  • 5
  • 26
  • 31
17
votes
5 answers

Is Test::Unit still relevant in rails?

I am learning Rails the age old way. By reading Agile Web Development with Rails (3rd Edition) as a starting point. I am currently in the chapter that teaches Testing. I am also aware of other BDD Testing framework such as RSPec. So I was wondering…
tundal45
  • 193
  • 3
  • 14
  • 33
17
votes
1 answer

How to use shoulda matchers to test a polymorphic association?

I'm using shoulda-matchers with rails and I'm creating a model called "comments" and another model called "post". Comments is polymorphic. When I test with shoulda matchers in post like this it {should have_many(:comments)} it get this message…
user651339
  • 171
  • 1
  • 3
17
votes
2 answers

How to test scopes?

tried to find but with no success. Just wondering how could I test scopes in Rails 3. Could be using rspec, shoulda or just a test unit. Thanks. Actually, I trying this way, but it's not complete test since it's still need to put the order()…
Kleber S.
  • 8,110
  • 6
  • 43
  • 69
1
2 3
25 26