Really? I just did a quick test:
public interface IFoo
{
string Name { get; set; }
int Age { get; }
}
[TestMethod]
public void StubReadOnlyProperty()
{
var foo = MockRepository.GenerateStub<IFoo>();
foo.Name = "bob";
foo.Stub(s => s.Age).Return(22);
}
And it worked fine. I'm also using VS2020, .NET 4 and Rhino.Mocks 3.6
(and Resharper too -- although that shouldn't affect anything).
You could also try putting the stub back into "record" mode and
setting up an expectation:
foo.BackToRecord();
foo.Expect(s => s.Age).Return(22);
foo.Replay();
But again, you shouldn't have to do that (but if it does work, at
least it would get you moving forward).
Can you throw the IFoo interface and unit test I created above into
your environment and see if it work?
---
Patrick Steele
http://weblogs.asp.net/psteele
On Fri, Mar 11, 2011 at 12:46 AM, Rob <[email protected]> wrote:
> Thanks for the response thats the code I had started with almost.
> I had tried that code the error that code produces.
>
> 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;
>
> Which is why I ended up where i was in initial post.
> I was hoping I could get closer to the form you present as i find it
> much easier to read.
>
> If I convert as per the error message I post above.
>
> var mockContext =
> MockRepository.GenerateStub<ICommunicationContext>();
> var mockPrincipal =
> MockRepository.GenerateStub<IPrincipal>();
> var mockIdentity =
> MockRepository.GenerateStub<IIdentity>();
>
> mockIdentity.Name = testUserName;
> mockPrincipal.Identity = mockIdentity;
> mockContext.User = mockPrincipal;
>
> In this case the Name, Identity and Request fields are all reported as
> not having a Setter.
> Property or indexer 'System.Security.Principal.IPrincipal.Identity'
> cannot be assigned to -- it is read only
>
> I am using Rhino Mopcks 3.6 with VS2010, Resharper 5.1 and in .Net 4
> mode. Though I dont believe that matters much.
> I did try disabling Resharper just in case it was doing something to
> muck up but had no change to behaviour.
>
>
> On Mar 11, 3:18 am, Patrick Steele <[email protected]> wrote:
>> var context = MockRepository.GenerateStub<ICommunicationContext>();
>> var principal = MockRepository.GenerateStub<IPrincipal>();
>> var identity = MockRepository.GenerateStub<IIdentity>();
>>
>> identity.Stub(i => i.Name).Return(testUserName);
>> principal.Stub(p => p.Identity).Return(identity);
>> context.Stub(c => c.User).Return(principal);
>
> --
> 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.
>
>
--
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.