Let me start by saying I am new to unit tests in general.
I'm trying to create a unit test for my BO using VS2008. I created a very simple test to just call my public factory method to return my Object. I am having a problem with my custom Identity, which makes sense that I would but I am lost as to how to get around it. Below is the error I am gettting.
DataPortal.Create failed (System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'Company.Library.Security.CIdentity'.
Any help would be appreciated.
-Stan
In nunit you need to "log in" before each test method. This could be (and probably is) the case with mstest too, because each test is run in an isolated environment, and so the principal is lost between each test.
In the case of nunit you also must log out after each test method or you get a serialization exception because .NET tries to serialize the principal back to the calling shell process, and that can't happen. I don't know if that's an issue with mstest - depends on how they manage their threads.
Thanks Rocky-
That's what I thought. I would like to test different Authorization levels in my test, and I was wondering if you have any thoughts on how I could accomplish this. I am using network authenication. I would like to have an XLS file or something with different username/password combination that the test could use to log on with to run the test. Could I use impersonation to accomplish this?
If you are using a custom principal (which you are from your
problem description), then you totally control how that principal is
established. If you create a DataPortal_Fetch() in your identity object that
can load from an XLS file or whatever, then you can do that. Or if you have a
separate DAL behind your identity object, then perhaps you can use a provider
model to load some other DAL implementation that reads the XLS instead of your
normal security database.
Or if you want to use your normal security database, then
perhaps you can have some code in your test project that loads the XLS and provides
the appropriate username/password at the start of each test.
Those options come to me off the top of my head anyway.
Rocky
Thanks.
I am experimenting with the last option. I am using Impersonation to change my identity at the start of each test, and I am reading the login information from an XLS file. This is all in a test environment, so I don’t have any security concerns about storing username/passwords in the XLS file. After I am done, I hope to find the time to create a sample to post back to the site.
Thanks for your help and guidance.
Stan
Copyright (c) Marimer LLC