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
4
votes
2 answers

Rails 3 custom validation and shoulda

Am swinging between Shoulda and Rspec these days. I have read and played around a fair bit with RSpec but not that much with Shoulda. I find Shoulda's one line assertions easier to read and the test looks cleaner. But when I can't figure out how…
MMinhas
  • 73
  • 2
  • 8
4
votes
1 answer

NoMethodError validate_presence_of using shoulda

Ok, I'm baffled. I'm trying to use shoulda with Test::Unit under Rails 3.1, having previously done so successfully with Rails 2.3.11. I have the following in my Gemfile: group :test do gem 'shoulda' end (and I've run bundle install - bundle show…
Chowlett
  • 45,935
  • 20
  • 116
  • 150
4
votes
2 answers

Shoulda superclass mismatch on unit tests

Trying to write a simple unit test using shoulda and rails 3. test/unit/user_test.rb class UserTest < Test::Unit::TestCase should validate_presence_of(:password, :on => :create) should validate_presence_of(:handle, :email) should…
Mike Sukmanowsky
  • 3,481
  • 3
  • 24
  • 31
4
votes
1 answer

How do I test that a before_filter will redirect for all Rails controller actions?

I have a fairly typical require_no_user as a before_filter in one of my controllers. I need to test that a logged in user is redirected by this filter if they try to access any of the controller's actions. Is there a sensible way to do this without…
nfm
  • 19,689
  • 15
  • 60
  • 90
4
votes
1 answer

Rails rspec shoulda matcher validating uniqueness of case insensitive item failing?

I'm trying to be a good rails developer and write tests as I go. I've run into something I'm unclear on and am looking for advice. I have a model that has a unique case insensitive attribute. The test is failing however. Whats the correct way to…
Jason Ellis
  • 625
  • 2
  • 8
  • 19
4
votes
1 answer

NoMethodError: undefined method `validate_presence_of' (Rspec and Shoulda-Matchers)

I'm trying to set up Rspec and Shoulda-Matchers, but for some reason I get this error: NoMethodError: undefined method `validate_presence_of' for #RSpec::ExampleGroups::AdCampaign::Validations:0x000000062a2b90> Failure/Error: it { should…
Denis Yakovenko
  • 3,241
  • 6
  • 48
  • 82
4
votes
1 answer

How to pass Rspec test has_many? [Shoulda-matchers]

I´m trying rails to develop a rest api. I´m using rspec and shoulda-matchers. The problem is that one of my test always fails. How can I correct my code, so the test passes (GREEN). user.rb class User < ActiveRecord::Base has_many…
Abraham
  • 71
  • 1
  • 6
4
votes
1 answer

Do shoulda-matchers' ActiveRecord matchers violate the "test behavior not implementation" rule?

For example, if I am using should validate_presence_of in my spec, that's only testing that I have the validate_presence_of piece of code inside my model, and that's testing implementation. More importantly, isn't that spec totally useless for…
4
votes
3 answers

Testing associations with rspec-rails 3.0.1 and shoulda doesn't work

Currently, with rspec-rails (2.14.2), I test my associations in model specs with the shoulda (3.5.0) gem like so: # app/models/user.rb class User < ActiveRecord::Base belongs_to :school end # spec/models/user_spec.rb describe User do it {…
Randy Burgess
  • 4,835
  • 6
  • 41
  • 59
4
votes
3 answers

How to test conditional validation with rspec-rails 3.0.0 and shoulda-matchers 2.5.0

I'm running a Rails 4 app with rspec-rails 3.0.0 and shoulda-matchers 2.5.0, and I've got an Event model that has some conditional validations like so: class Event < ActiveRecord::Base validates :name, :location, :time, :city, :zipcode, :user,…
Tyler
  • 135
  • 2
  • 6
4
votes
1 answer

Rails 4 syntax for shoulda matchers "have_many" with "order"

I have a rails 4 validation for my User model: has_many :items, -> { order(:position) } I expected the following shoulda matcher to work: it {should have_many(:items).order(:position)} But it raises this error: Expected User to have a has_many…
4
votes
3 answers

Print a List of All Tests in a Rails App

Is there an easy ruby or rails command to get a list of all the tests in an application? I use Shoulda primarily, but if there's a ruby solution regardless of test suite that would work too.
Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50
4
votes
1 answer

how to change subject for shoulda matchers

I'm having trouble with the following attributes within my class. I have a date attribute with presence true that is enforced at database level too: validates :date, presence: true In addition to that, I need to enforce combined uniqueness for the…
Pablo
  • 3,433
  • 7
  • 44
  • 62
4
votes
1 answer

rSpec Testing Rails using Shoulda matchers

I have been starting to learn testing in my Rails app and am using rSpec and Shoulda. I have the following test which works: it { should respond_to(:park_name) } However, what I don't understand is, what is this being run on? Is this being run on…
rctneil
  • 7,016
  • 10
  • 40
  • 83
4
votes
3 answers

Should I mock my model in rails controller tests?

I am finding holes in my coverage because I have been mocking my models in controller examples. When I remove a model's method upon which a controller depends, I do not get a failure. Coming from TDD in statically typed languages, I would always…
Lee
  • 9,432
  • 3
  • 30
  • 34