Skip to content

Instantly share code, notes, and snippets.

@prathamesh-sonpatki
Last active December 11, 2015 05:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prathamesh-sonpatki/4554216 to your computer and use it in GitHub Desktop.
Save prathamesh-sonpatki/4554216 to your computer and use it in GitHub Desktop.
Shoulda-matchers validation error
class Email < ActiveRecord::Base
scope :by_account, -> account_id { where(account_id: account_id) }
attr_accessible :template_name, :subject, :content, :user_id, :account_id
validates :subject, presence: true
validates :template_name, presence: true
validates :template_name, uniqueness: { :scope => :account_id }
validates :content, presence: true
has_many :links, dependent: :destroy
belongs_to :account
belongs_to :user
end
require 'spec_helper'
describe Email do
describe "Validations" do
context "when created with invalid attributes" do
it "will not be created without :subject" do
should validate_presence_of(:subject)
end
it "will not be created without :template_name" do
should validate_presence_of(:template_name)
end
it "will not be created without :content" do
should validate_presence_of(:content)
end
it "does not allow emails with duplicate template_name for a account" do
should validate_uniqueness_of(:template_name).scoped_to(:account_id)
end
end
end
end
Failures:
1) Email Validations when created with invalid attributes does not allow emails with duplicate template_name for a account
Failure/Error: should validate_uniqueness_of(:template_name).scoped_to(:account_id)
ActiveRecord::StatementInvalid:
PG::Error: ERROR: null value in column "subject" violates not-null constraint
: INSERT INTO "emails" ("account_id", "archive", "as_web_page", "attachment_url", "bcc", "cc", "contact_id", "content", "created_at", "current_tab", "from", "latest_activity_date", "status", "subject", "template_name", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING "id"
# ./spec/models/email_spec.rb:30:in `block (4 levels) in <top (required)>'
Finished in 5.44 seconds
135 examples, 1 failure, 2 pending
Failed examples:
rspec ./spec/models/email_spec.rb:29 # Email Validations when created with invalid attributes does not allow emails with duplicate template_name for a account
Randomized with seed 45615
@robbl
Copy link

robbl commented May 16, 2013

Hey, I'm having the same issue with the shoulda-matchers. Have you found any solution or workaround to that problem, yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment