As I suspected the use of Auto-Props here messed you up.
This code works for me: *Use caution when using auto props and virtual!!!*
Two things to note about my code below:
- I removed the call to validator. It wasn't causing the test to fail,
so I removed it.
- I removed the private set and exposed onlly a public getter and backed
the property with a private variable.
[TestFixture]
public class PeterTests
{
[Test]
public void ReturnsRequired_ForEmailAddress_WhenNull()
{
User mockUser = MockRepository.GenerateStub<User>("");
mockUser.Stub(x => x.EmailAddress).Return(null);
Console.WriteLine(mockUser.EmailAddress);
}
public class User
{
private string emailAddress;
public virtual string Title { get; set; }
public virtual string GivenName { get; set; }
public virtual string FamilyName { get; set; }
public virtual string EmailAddress
{
get { return emailAddress; }
}
public User(string emailAddress)
{
this.emailAddress = emailAddress;
}
}
}
On Sun, Jun 7, 2009 at 7:32 AM, Tim Barcz <[email protected]> wrote:
> Also auto props are great bit your setting the value of a virtual
> property in your constructor. That's a no-no.
>
> (Thinking out loud) I'm thinking this may be your issue since dynamic
> proxy will want to subclass User but is running into issues with
> setting the email address since it is virtual in the base class.
>
> On 6/7/09, Peter Morris <[email protected]> wrote:
> >
> > Here is my test...
> >
> > [TestMethod]
> > public void ReturnsRequired_ForEmailAddress_WhenNull()
> > {
> > User mockUser = MockRepository.GenerateStub<User>("");
> > mockUser.Stub(x => x.EmailAddress).Return(null);
> > Validator.GetErrors(mockUser)
> > .Where(e => e.FieldName == "EmailAddress" && e.Error ==
> > "Required").Single();
> > }
> >
> > Results in this exception:
> >
> > Test method
> >
> TaskSmart.DomainClasses.Tests.UserValidatorTests.ReturnsRequired_ForEmailAddress_WhenNull
> > threw exception: System.InvalidOperationException: You are trying to
> > set an expectation on a property that was defined to use
> > PropertyBehavior.
> > Instead of writing code such as this: mockObject.Stub(x =>
> > x.SomeProperty).Return(42);
> > You can use the property directly to achieve the same result:
> > mockObject.SomeProperty = 42;.
> >
> > I think the error message is incorrect. I cannot set the property
> > because it has a private setter...
> >
> > namespace TaskSmart.DomainClasses
> > {
> > public class User
> > {
> > public virtual string Title { get; set; }
> > public virtual string GivenName { get; set; }
> > public virtual string FamilyName { get; set; }
> > public virtual string EmailAddress { get; private set; }
> >
> > public User(string emailAddress)
> > {
> > EmailAddress = emailAddress;
> > }
> > }
> > }
> >
> > So how should I get this test to work? I don't want to have to define
> > interfaces for my domain objects just to achieve testing.
> >
> >
> > Thanks
> >
> > Pete
> > > >
> >
>
> --
> Sent from my mobile device
>
> Tim Barcz
> ASPInsider
> http://timbarcz.devlicio.us
> http://www.twitter.com/timbarcz
>
--
Tim Barcz
ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---