views:

197

answers:

2

I have a test that requires me to uses Authentication to check to see if a user has logged in and authenticated before performing its task.

Is it possible for me to set the Authentication from the unit test so as to fool / mock the Authentication in the main application to thinking you are authorised and logged in?

+2  A: 

What I would do in this case is have a method that takes an IIdentity instance. You can then use any mocking framework (such as Rhino Mocks) to mock IIdentity in order to ensure that the "user" is logged in or not logged in.

public void DoSomething(IIdentity identity)
{
    if(identity.IsAuthenticated) ...
}

and then your unit test would look like:

[Test]
public void Test()
{
     var mockery = new MockRepository();
     IIdentity identity = mockery.DynamicMock<IIdentity>();

     //perform your test logic here
}
bdowden
Yes I can see how you have approached this, the issue is I am using LINQ to SQL and the DoSomething() will not allow any parameters, as it is:partial void OnCreated() {
Coppermill
A: 

One solution is to have the web.config file on the testing machine have it's authorization element set to allow all users. This assumes your test machine and production machine have incompatible web configs (i.e. if you inadvertently upload the no-authenticate web.config to the production machine, it should break everything rather than just remove authentication).

This might be considered bad practice...

Brian