2

I have this type:

[RequiresAuthentication]
public class MobileRunReportHandler : IMobileRunReportHandler
{
  public void Post(MobileRunReport report)
  {
    ...
  }
}

I am mocking it like so:

var handler = MockRepository.GenerateStub<IMobileRunReportHandler>();
handler.Stub(x => x.Post(mobileRunReport));

The problem is that the produced mock is not attributed with the RequiresAuthentication attribute. How do I fix it?

Thanks.

EDIT

I want the mocked type to be attributed with the RequiresAuthentication attribute, because the code that I am testing makes use of this attribute. I would like to know how can I change my mocking code to instruct the mocking framework to attribute the produced mock accordingly.

mark
  • 59,016
  • 79
  • 296
  • 580
  • 1
    possible duplicate of [Mocking attributes - C#](http://stackoverflow.com/questions/319002/mocking-attributes-c) – msarchet Sep 28 '11 at 19:38
  • I will gladly agree with you if you show me where exactly does that post contain the answer to my question. Thanks. – mark Sep 28 '11 at 20:16
  • In what context do you need the class to be attributed with `RequiresAuthentication`? Could you elaborate that a little more? – Jeroen Sep 29 '11 at 07:06
  • How does the code you are testing use the attribute? Could you add a code snippet? The approach to add the attribute to the stub might depend on this. – Jeroen Oct 06 '11 at 08:24
  • This happens inside the OpenRasta framework. It uses the regular reflection API - Attribute.GetCustomAttributes. – mark Oct 06 '11 at 09:43

1 Answers1

1

Adding an Attribute to a type at runtime and then getting it using reflection isn't possible (see for example this post). The easiest way to add the RequiresAuthentication attribute to the stub is to create this stub yourself:

// Define this class in your test library.
[RequiresAuthentication]
public class MobileRunReportHandlerStub : IMobileRunReportHandler
{
    // Note that the method is virtual. Otherwise it cannot be mocked.
    public virtual void Post(MobileRunReport report)
    {
        ...
    }
}

...

var handler = MockRepository.GenerateStub<MobileRunReportHandlerStub>();
handler.Stub(x => x.Post(mobileRunReport));

Or you could generate a stub for the MobileRunReportHandler type. But you'd have to make its Post method virtual:

[RequiresAuthentication]
public class MobileRunReportHandler : IMobileRunReportHandler
{
    public virtual void Post(MobileRunReport report)
    {
        ...
    }
}

...

var handler = MockRepository.GenerateStub<MobileRunReportHandler>();
handler.Stub(x => x.Post(mobileRunReport));
Community
  • 1
  • 1
Jeroen
  • 1,246
  • 11
  • 23
  • I am sorry. I just do not get it. The stub generated by Rhino.Mocks is of dynamic type, i.e. the type is created at runtime using the `Reflection.Emit` facility, which allows to have a dynamic type attributed as we please. It is all a question of whether Rhino.Mocks knows to exploit this ability of `Reflection.Emit` or not. – mark Oct 06 '11 at 13:02