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

How to use ensure_inclusion_of for an arbitrary list

In my rails project I have a model with attribute foo. I have a vaildation which ensures that any value for foo is in an array. This uses a validation with the :in option. I want to test this using rspec/shoulda. I think ensure_inclusion_of is…
Zameer Manji
  • 3,017
  • 5
  • 31
  • 42
2
votes
1 answer

Is there a way to run a single focused Shoulda test or context in Vim (possibly with rails.vim)?

The only thing I haven't been able to do with Vim that I was able to do with TextMate is run a focused test or context when using Shoulda. :Rake runs the current file (which still works fine) The help docs on :Rake say this: Additionally, when…
solidcell
  • 7,639
  • 4
  • 40
  • 59
2
votes
1 answer

Using Shoulda redirect_to to test a controller's create action

I'm using RSpec + Shoulda to test my RESTful controller in Rails 3. I'm having trouble figuring out how to test the create action's redirect. The standard RESTful controller should redirect to the show action for the new post. For example, if I…
Rick
  • 58
  • 8
2
votes
6 answers

What's the use of writing tests matching configuration-like code line by line?

I have been wondering about the usefulness of writing tests that match code one-by-one. Just an example: in Rails, you can define 7 restful routes in one line in routes.rb using: resources :products BDD/TDD prescribes you test first and then write…
Pascal Van Hecke
  • 4,516
  • 3
  • 19
  • 18
2
votes
1 answer

Integration tests with webrat and Rails3

I'm upgrading a Rails 2.3.5 app to Rails 3.0.3. But my integration tests aren't working. I'm getting this error: NoMethodError: undefined method `content_type' for nil:NilClass The line to blame is assert_select "input#artist.title.unsolved", 1 My…
23tux
  • 14,104
  • 15
  • 88
  • 187
2
votes
1 answer

Shoulda Test in Rails 3: should respond_with :success

I have some troubles with testing in Rails 3. I'm currently upgrading a Rails2 app to Rails3. I'm using shoulda for testing. In my functional tests, I'm testing with shoulda, that a GET should respond with success context "GET to :blame" do should…
23tux
  • 14,104
  • 15
  • 88
  • 187
2
votes
0 answers

shoulda matcher and conditional validation errors

Here I use the attribute :new_record to set the condition for validation. attr_accessor :new_record validates :email, presence: true, email: true, unless: :new_record? def new_record? @new_record || true end if the new_record? == true, the…
Ian Tran
  • 315
  • 3
  • 14
2
votes
1 answer

How to create unit test for Paperclip::Geometry?

I'm writing unit test for a image uploader that uses paperclip. It's being bumpy and I was slowly moving thought the hurdles till I got stuck in the paperclip::geometry class Here is my code below require 'test_helper' require…
DigMe
  • 21
  • 1
2
votes
2 answers

FactoryGirl::InvalidFactoryError: Validation failed: must exist (ActiveRecord::RecordInvalid)

I have these two models class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :posts end class Post < ApplicationRecord belongs_to…
ltdev
  • 4,037
  • 20
  • 69
  • 129
2
votes
1 answer

Rspec test belongs_to with shoulda_matchers

I have this code in my model. belongs_to :user, -> { includes :user_profile } I went through shoulda doc i did not found any way to test that proc containing includes part. Please help.
Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
2
votes
2 answers

How to test a model instance method which makes a call to an external API

I am having trouble understanding what to test in the case below and how to do it. I have the following instance method on Address model validate :address, on: [:create, :update] def address check =…
Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82
2
votes
1 answer

Shoulda with Rspec Gem returning "undefined method `reflect_on_association' for String:Class" in belong_to test

In my rails app, I have my models Request, Service, and ServiceRequest In my models.rb files I have: request.rb: class Request < ApplicationRecord validates_presence_of :userid, :supervisor, :status has_many :servicerequests, dependent:…
Avir94
  • 899
  • 9
  • 19
2
votes
1 answer

Rails 4 - undefined method 'authenticate' for nil:NilClass

I'm working on a course building a social network using Rails 4 & Devise. I'm stuck trying to get a course to pass, this is the error I'm getting: 1)…
mycellius
  • 568
  • 1
  • 5
  • 28
2
votes
2 answers

Shoulda have_many with class_name, foreign_key and primary_key

How do I test this ActiveRecord relation using ShouldaMatchers? Models class ViolatorUnitHistory < ActiveRecord::Base ... belongs_to :primary_source, class_name: Source, primary_key: :source_1_id, foreign_key: :id belongs_to :secondary_source,…
ckib16
  • 383
  • 4
  • 8
2
votes
0 answers

Validation fails with delegation error

I have the following model associations class WarehouseVariant < AB has_one :warehouse has_one :variant has_many :variant_cities end class VariantCity < AB belongs_to :warehouse_variant delegate :mrp_paise, :to => :warehouse_variant …
MIdhun Krishna
  • 1,739
  • 1
  • 13
  • 31