Validate uniqueness depends on record

20 views
Skip to first unread message

rejeep

unread,
Nov 22, 2009, 11:42:17 AM11/22/09
to shoulda
Hi!

I have this Request model that I want to test a little bit different
depending on an attribute. The attribute is *local*. So if a Request
is local, I don't want to validate uniqueness of *url*. But I do want
that if the Request is global.

So I thought I would do it like this:
class RequestTest < ActiveSupport::TestCase
subject { Factory(:request) }

context "a local request" do
setup { subject { Factory(:request, :local => true) } }

# No uniqueness validation
end

context "a global request" do
setup { subject { Factory(:request, :local => false) } }

should_validate_uniqueness_of :url
end

# ...
end

But this does not work.

I also tried to stub the subject block:
context "a global request" do
setup do
request = Factory(:request, :local => false)
stubs(:subject).returns(request)
end

should_validate_uniqueness_of :url
end

But no success there either...

Do you know how to solve this?

Thanks!

François Beausoleil

unread,
Nov 22, 2009, 10:58:00 PM11/22/09
to sho...@googlegroups.com
You don't show any error messages, so I'm not too sure. I believe #subject is a class method, so you'd do it this way:

context "a global request" do
subject do
@subject ||= Factory(:request, :local => false)
end

should_validate_uniqueness_of :url
end

RDoc for #subject: http://rdoc.info/rdoc/thoughtbot/shoulda/blob/21ce037d1228a5f56deac79c55049e33f8dda5ae/Shoulda/ClassMethods.html#subject-instance_method

Hope that helps!
François
> --
> Individuals over processes. Interactions over tools.
>
> Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
> http://thoughtbot.com/services/training
>
> The Shoulda group:
> http://groups.google.com/group/shoulda
>
> To post to this group, send email to
> sho...@googlegroups.com
>
> To unsubscribe from this group, send email to
> shoulda+u...@googlegroups.com

rejeep

unread,
Nov 23, 2009, 1:42:31 AM11/23/09
to shoulda
Sorry if I was unclear. There's no error, but the test fails with
this:
Expected errors to include "has already been taken" when url is set to
"http://domain.com", got no errors

Your suggestion didn't work.

On Nov 23, 3:58 am, François Beausoleil
<francois.beausol...@gmail.com> wrote:
> You don't show any error messages, so I'm not too sure.  I believe #subject is a class method, so you'd do it this way:
>
> context "a global request" do
>   subject do
>     @subject ||= Factory(:request, :local => false)
>   end
>
>   should_validate_uniqueness_of :url
> end
>
> RDoc for #subject:http://rdoc.info/rdoc/thoughtbot/shoulda/blob/21ce037d1228a5f56deac79...

rejeep

unread,
Nov 24, 2009, 2:30:42 AM11/24/09
to shoulda
Maybe there's a better way to do it? What I'm trying to test is this:
validates_uniqueness_of :url, :unless => :local?
validates_uniqueness_of :url, :if => :local?, :scope => :user_id
Reply all
Reply to author
Forward
0 new messages