tags:

views:

202

answers:

2

Trying to get Nunit working in ASP.Net.

The problem is, I'm testing a custom control - which references a Global Resource.

When I try to unit test it

/// </summary>
        [Test]
        public void TestSetAndGetNumber()
        {
            PhoneNumber phone = new PhoneNumber(PhoneNumber.NumberType.Business, "", true, "");
            string expectedString = "1-800-Goat-Phone";
            string resultString = "1-800-Goat-Phone";
            resultString = phone.Value = resultString;
            Assert.AreEqual(expectedString, resultString, "GetNumberMatch method returned unexpected result.");
            Assert.Fail("Create or modify test(s).");
        }

I will get "cannot load App_GlobalResources".

Trying to figure out if maybe I should attempt to simulate HttpContext Using You've been Haacked blog post but nobody has verified this is do-able.

+1  A: 

This may sound glib, but can't you just redesign the PhoneNumber class so that it is testable?

If you want to use unit testing, this would seem to be the way to go. If you can't modify the PhoneNumber class, what's the point in testing it?

Joe
+1  A: 

This is a typical scenario that you should set up your class in a right way to be testable. If you have external dependency (references to a Global Resource), then you should make it an abstract, so you can mock it.

I suggest you take a look at this book, and it explains how to design your class be more testable. Alternative, you can download examples from moq. Set up unit testing is a lot about designing your class in more loose coupled way, so you can effectively test it without interaction with DB/File system/other resource.

J.W.
The tested class is a custom control, PhoneNumber. All instances of phone number currently require a hit to GlobalResources to get label text, etc. I'm not sure how you would recommending changing the class so that you can test parts of it without hitting GlobalResource. If PhoneNumber used an interface to access GlobalResources, I don't see how to have the unit test cleanly change that interface to return mock data.
scottschulthess
what is your purpose of testing? you want to test the string content in GlobalResource? I am a bit confused with what you exactly want to test?
J.W.
some logic in the getter-setter. i realize that some people's response to this would be to have no logic in the getter setter...but it's getter/setter type logic (display logic). i'm not a huge fan of having interfaces that provide little value from a design pattern standpoint. really i would just like to simulate httpcontext in the unit test, but maybe I am wishing on a star.
scottschulthess
Can you extract "getter/setter type logic" into different method? My point is , you only need to test your logic, NOT the httpcontext logic. So if you just need some properties from httpContext, then extract it and call your display logic method.
J.W.
@J.W. You're correct!
scottschulthess