ValidateIsUnique vs. Testing

2 views
Skip to first unread message

James Thigpen

unread,
Sep 9, 2008, 8:35:10 PM9/9/08
to Castle Project Users
Hello all,

I want to use the ValidateIsUnique attribute, but that means when
testing my models I have to have bring up the entire database and init
activerecord etc. even when I'm not testing my persistence layer
which is slow and undesirable. I'm using ActiveRecordMediator based
repositories.

Is there any way around this? Perhaps this is just an indicator that
"ValidateIsUnique" maybe doesn't belong in my domain models? It's a
database concern I guess, but it's terribly convenient to be able to
have it there and get the error message when I'm doing form
submission.

Any suggestions greatly appreciated.

Thanks,

-jt

Eric Hauser

unread,
Sep 10, 2008, 1:16:09 PM9/10/08
to Castle Project Users
There are plenty of hacks you could do so that it does not run when
you are running your unit tests...

One way would be to create a new validation registry that mimics the
behavior of CachedValidationRegistry, but ignores that particular
validator. It is a little bit of cut and paste work becaused
CachedValidationRegistry's methods are not virtual. Then in your test
setup, you can do:

controller.Validator = new ValidatorRunner(new
MyValidationRegistry());

Another way would be to override the ValidateIsUniqueAttribute class
and have the constructor check a variable to see if if should create
the IsUniqueValidator or not. Pseudo code:

MyValidateIsUniqueAttribute : ValidateIsUniqueAttribute

public MyValidateIsUniqueAttribute()
validator = Local.Data["IsUnitTest"] ? new
AlwaysReturnsNoErrorsValidator() : new IsUniqueValidator();

Victor Kornov

unread,
Sep 10, 2008, 1:33:37 PM9/10/08
to castle-pro...@googlegroups.com
Confirmed, that works. I've moved from ValidateIsUniqueAttribute later anyways.

James Thigpen

unread,
Sep 11, 2008, 9:57:59 AM9/11/08
to Castle Project Users
This worked great. I ended up putting a static property on my custom
ValidateIsUniqueSometimes attribute to enable or disable it.

Thanks so much!

-jt

On Sep 10, 10:33 am, "Victor Kornov" <wee...@gmail.com> wrote:
> Confirmed, that works. I've moved from ValidateIsUniqueAttribute later
> anyways.
>
Reply all
Reply to author
Forward
0 new messages