This is a migrated thread and some comments may be shown as answers.

mocking CustomeAttributes

5 Answers 118 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 24 Jul 2014, 01:30 PM
I am trying to mock this for a unit test... I need help getting all items in here mocked. what I have done does not work and it seems like that the mocks never get hit when they are supposed to. I know that a few of the items are part of our library and may not work on your  machines but if you could help please get it on track and I can run it and straighten it out if needed.

public static string GetEnumDisplayName<T>(string enumName)
        {
            string retVal = null;
            MemberInfo memberInfo = typeof(T).GetMember(enumName).FirstOrDefault();
            if (memberInfo != null)

            {
                object[] customAtts = memberInfo.GetCustomAttributes(typeof(IDisplayName), false);
                if (customAtts != null && customAtts.Length > 0)
                {
                    retVal = ((IDisplayName)customAtts[0]).DisplayName;
                }
                //
                if (retVal == null)
                {
                    customAtts = memberInfo.GetCustomAttributes(typeof(GeneralDisplayNameAttribute), false);
                    if (customAtts != null && customAtts.Length > 0)
                    {
                        retVal = ((GeneralDisplayNameAttribute)customAtts[0]).DisplayName;
                    }
                }
            }
            return retVal;
        }

5 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 24 Jul 2014, 02:41 PM
Hello Matthew,

Mocking this method is pretty straightforward:
Mock.Arrange(() => Util.GetEnumDisplayName<MyEnum>("Foo")).Returns("The Foo");
var displayName = Util.GetEnumDisplayName<MyEnum>("Foo");
Assert.AreEqual("The Foo", displayName);
or maybe with an IsAny matcher:
Mock.Arrange(() => Util.GetEnumDisplayName<MyEnum>(Arg.AnyString))
    .Returns((string arg) => "The " + arg);
var displayName = Util.GetEnumDisplayName<MyEnum>("Foo");
Assert.AreEqual("The Foo", displayName);

Note that currently there's no syntax for mocking generic methods "for all type arguments". You must arrange the method separately for each type you expect to be used as a type argument to the method. Is this what you want to be able to do?

I hope the above helps you with writing your test.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Matthew
Top achievements
Rank 1
answered on 25 Jul 2014, 02:28 PM
I feel that I am missing something here. To me at least, all the mock does is say that when the GetEnumDisplayName is called return this string back and then match it. How does this test any of the logic posted? If I am not missing anything, then all the unit test would test for is if the method can be called? I actually need to be able to test all aspects of the method for the test be considered a useful test. So from my limited knowledge of this memberinfo may need to mocked up so that when GetCustomAttributes is called the call is then overridden with a functioning value.

If I am missing something please let me know.
0
Stefan
Telerik team
answered on 28 Jul 2014, 03:29 PM
Hi Matthew,

It appears that I've misunderstood your requirements. I though you wanted to mock this method. Instead, you want to test the method and that's why you're trying to mock its dependencies.

While it is technically possible to mock all of the method's dependencies in the reflection namespace, I would advise you to write a test without mocking. Simply add an enum with elements decorated with IDisplayName-derived attributes to your test fixture and exercise the method using that test enum. It will result in a test that is easier to understand and maintain.

Mocking as a technique is generally reserved to cases when it's much easier to recreate a dependency's necessary behavior for the test using a mock than it is to set up the real dependency and give it the test behavior. In your case it is trivial to create the test data for your method and to exercise it using hat data and it is relatively hard to mock the guts of the reflection namespace, so there's no merit to using mocking in this case.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Matthew
Top achievements
Rank 1
answered on 28 Jul 2014, 06:31 PM
the issue that I see is that IDisplaName is an interface. I  have had to mock these a few times. 

Any way could you either show how to mock it or link a good example that I can use to finish this test? this is the last test I need to do for this project. I also tried to research this and could not find a way to do it. If you had a good resource for me to view It would be very helpful.

Thank you for all the help so far.
0
Matthew
Top achievements
Rank 1
answered on 29 Jul 2014, 03:23 PM
Never mind I figured it out. Thanks for the help though. I am I will be back as soon as I hit another wall on some other tests
Tags
General Discussions
Asked by
Matthew
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or