0

How do you mock out the validates :name, uniqueness: true callback for an attribute?

I imagine it would work something like this:

describe "with duplicate name" do
  before { mock(subject.name).unique? { false } }

  it "should be invalid" do
    subject.should have(1).error_on(:name)
  end
vladiim
  • 1,862
  • 2
  • 20
  • 27

1 Answers1

1

Why not using shoulda matchers? With it you can test standard common rails functionality with one-liners like it { should validate_uniqueness_of(:name) }. I find it more readable and eliminates the need for mocking callbacks and preparing test cases

Wei
  • 1,252
  • 13
  • 19
  • Thanks Wei. Shoulda's definitely clean but I don't think I can justify ripping out the rest of my tests for this one spec! – vladiim Jul 30 '12 at 11:34
  • What do you mean by ripping out the rest of your tests? like other validations and such tested through mocking? – Wei Jul 30 '12 at 15:01