Skip to content

Instantly share code, notes, and snippets.

@adimircolen
Created May 6, 2011 20:16
Show Gist options
  • Save adimircolen/959703 to your computer and use it in GitHub Desktop.
Save adimircolen/959703 to your computer and use it in GitHub Desktop.
validates_associated
class Customer < ActiveRecord::Base
has_many :projects
validates :name, :presence => true
validates :cnpj, :presence => true, :format => { :with => /^\d{2}.?(\d{3}.?){2}\/\d{4}-?\d{2}$/ }
end
Project
is valid
with valid attributes
is not valid
without a name
without a customer (FAILED - 1)
Failures:
1) Project is not valid without a customer
Failure/Error: subject.save.should be_false
expected true to be false
# ./spec/models/project_spec.rb:25:in `block (3 levels) in <top (required)>'
Finished in 0.22772 seconds
3 examples, 1 failure
class Project < ActiveRecord::Base
belongs_to :customer
validates :name, :presence => true
# validates :customer, :presence => true
validates_associated :customer
end
require 'spec_helper'
describe Project do
context "is valid" do
subject do
Project.new(name: "some name", customer: nil)
end
let(:customer){Factory(:customer)}
it "with valid attributes" do
# subject.customer = customer
subject.customer = nil
subject.save.should be_true
# subject.should be_valid
end
end
context "is not valid" do
it "without a name" do
subject.should_not be_valid
end
it "without a customer" do
subject.name = "some other name"
subject.customer = nil
subject.save.should be_false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment