views:

230

answers:

2

Hello,

I need to fake HttpContext.Current.Application table to access it from my unit tests.

I need to store my data somewhere. I thought that I can just pass instance of NameValueCollectionBase but as I descover this base type has no indexer so it's too complicated to use.

So what about faking this part of HttpContext? Is it possible? How can I make it? Will be NUnit.Mocks helpful?

Thank you in advice...

+2  A: 

Hello,

Please go through below links it will help you.

http://www.java2s.com/Open-Source/CSharp/Web-Frameworks/MvcContrib/MvcContrib/TestHelper/Fakes/FakeHttpContext.cs.htm

http://stackoverflow.com/questions/677801/mocking-and-httpcontextbase-get-user

Thanks Venkat

Venkat
Problem is that if it's about `User` then it's easy to return new object of type `IPrincipal`, but when it comes to `Application` then I need to return some `HttpApplicationState` object which has no public constructor and because of it's use throught indexers it is hard to fake with `NUnit.Mocks`... Can you provide me some code example for this case?
ŁukaszW.pl
Hello,If you need indexes for namevaluecollection base please use below codepublic static IEnumerable<KeyValuePair<string, string>> ToPairs(this NameValueCollection collection){ if(collection == null) { throw new ArgumentNullException("collection"); } return collection.Cast<string>().Select(key => new KeyValuePair<string, string>(key, collection[key]));}For just to store data and passing around test methods please use above code.
Venkat
+1  A: 

Hello,

If you need indexes for namevaluecollection base please use below code

public static IEnumerable<KeyValuePair<string, string>> ToPairs(this NameValueCollection collection)
{
    if(collection == null)
    {
        throw new ArgumentNullException("collection");
    }

    return collection.Cast<string>().Select(key => new KeyValuePair<string, string>(key, collection[key]));
}

For just to store data and passing around test methods please use above code.

Venkat