Hello.

Who can help me with UnitTest in C # / MVC?

I do not understand how Exception can be fixed, it comes from the fact that my written program ( UnitTest ) is not complete.

I am testing the Login() function, setting the "email" and "password" at the entrance, the method in the Unit Test-e works separately from the project, and for this it is necessary for the project to work in parallel to communicate with the base / Global.asax file (5 figure)

Global file

Return values ​​"email" and "password" -> null (when doing debag), no connection to the server, and always generated

Exception Message: "Test method UnitProject.UnitTest.TestLoginUser threw exception: Ogma.Project.Common.Security.LoginException: There is an error on server side."

I think the problem is in the ConnectionString (2 image)

Connectionstring

Returns null during debug (3 figure)

ConnectionString returns null

But I have a ConnectionString (4 image)

I have a connectionstring

The test is written for AccountController . The test run code (1 image)

Run Test Code

    1 answer 1

    See what's the matter.

    When your application starts up in the usual way, Application_Start is executed, in which the necessary configuration takes place. But in the case of tests, your code runs in the context of the test application, and you need to initialize separately.

    To do this, you must create a method in which customization will be made, and apply one of the attributes [AssemblyInitialize] , [ClassInitialize] or [TestInitialize] to it, depending on whether you want to set parameters for the entire test assembly, for one class or for one test (in your case, apparently, the first option takes place).

    In addition, if you want to use real connection strings (and why is it for tests?), You will also have to put these same strings in the place available for the test. That is, add app.config to your project, and transfer to it a string from web.config (probably the entire connectionStrings section). But most likely for tests you do not need a real, “combat” connection, so most likely you will need to put a special string for the test connection.