Below code reproduces the bug precisely:
public class Dog
{
public virtual int Speed { get; set; }
public virtual bool Active { get; set; }
}
public interface IDogDao
{
Dog GetDog();
}
public class DogDao : IDogDao
{
public Dog GetDog() { throw new NotImplementedException(); }
}
public class MyService
{
public IDogDao DogDao { get; set; }
public void Manipulate()
{
Dog dog = DogDao.GetDog();
if (dog.Active)
{
dog.Speed++;
}
else
{
dog.Speed = 0;
}
}
}
public class MyService_test
{
[Test]
public void Manipulate_active_dog()
{
var dog = new Dog() {Active = true, Speed = 1};
var mocks = new MockRepository();
var dao = mocks.Stub<IDogDao>();
dao.Stub(x => x.GetDog()).Return(dog);
MyService svc = new MyService() {DogDao = dao};
svc.Manipulate();
Assert.AreEqual(2, dog.Speed);
}
}
If I take the Virtual off of my Dog class then it doesn't throw an
exception but my test fails because Rhino Mocks is sending a new Dog
object with default values (not the values I set).
On Oct 4, 6:05 pm, Tim Barcz <[email protected]> wrote:
> Please post the failing code/test (that always helps).
>
> Tim
>
>
>
>
>
> On Sun, Oct 4, 2009 at 6:03 PM, Scott White <[email protected]> wrote:
>
> > I was migrating some of my R&P methods into stubs but one of them I
> > could not convert over. This I think is because the return value is a
> > class with all virtual properties and Rhino Mocks seems to be proxying
> > it and not returning the values that I set.
>
> > dao.Stub(x => x.FindByUser(user.GetType(), user.Username)).Return
> > (user); // dao.Expect has same behavior
>
> > later in my test:
>
> > service.Authenticate(..)
>
> > This service calls the dao and takes the user object and maniuplates
> > it if necessary. Problem with this mechanism is that I'm getting an
> > exception
>
> > Previous method 'AppUser.get_Active();' requires a return value or an
> > exception to throw.
>
> > On a check of the Active flag within my Authenticate function. For
> > the time being I've reverted back to recording this expectation. Any
> > idea why Rhino Mocks is proxying my user object that is being returned
> > or how to prevent it from doing so?
>
> --
> Tim Barcz
> Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://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
-~----------~----~----~----~------~----~------~--~---