Why can't my integration tests in Rider recognize App.Config file? - c#

I have a C# project that I'm working on in Rider. Within this project I have an Integration Test project. When I go to run my integration tests, they fail because they call code that depends on information in the App.config file. I have made a linking reference in the integration test project to the App.config file from the main project. The integration tests still do not work. They throw ArgumentNullExceptions for any values that are stored in the App.config file.
The main project runs fine and retrieves the values from the App.config file perfectly.
I have also tried rebuilding integration test project.
Whether I copy the App.config file over or make a linking reference to it, I get the same results.

Related

Runtime folder structure when running xUnit.net tests

I'm working on a C# web app running on .net 4.6.1 that assumes that certain dlls are in the same folder as the executing assembly and tries to import some types from those dlls using MEF. That works fine when the app is running and all the required dlls are in the bin folder, but I have problems when running xUnit tests that eventually get to run the code that makes the assumption mentioned above. I'm running the tests using Visual Studio 2017.
The problem I have is that when I run an xUnit test it creates a temp folder which contains a subfolder for each reference I have in my unit test project. In every subfolder there is the .dll and the .pdb file of a single reference along with an __AssemblyInfo__.ini file. Obviously, the test crashes with a FileNotFoundException because the code that wants to import some types using MEF cannot find the dlls in the same folder as the executing assembly location.
Is there a way to tell xUnit to put all the references of the unit test project in a single folder (like the bin folder?) and run the tests using that folder? Or is this maybe a setting in Visual Studio 2017?
You can simply disable shadowCopy, so that all execution uses your output folder.
To learn how to configure xUnit.net via JSON, use a search engine.

MSB1003 when building ASP with Jenkins

I am using Jenkins as a Windows Service with MSBuild to try and build an ASP project, but I get the following error after Jenkins have fetched all the source files from TFS:
MSBUILD : error MSB1003: Specify a project or solution file. The
current working directory does not contain a project or solution
file.
I have 3 projects in my solution. One that is ASP, one that is C# only and one that is a Database project. I have already set in my Solution that the ASP project should be the startup one but I guess I need to include the information somewhere else?
I don't quite understand where it wants me to set the project or solution file to make this work ._.
I have other projects from Colleagues at work as well whose projects are compiled and analyzed just fine. One of them is a heavy ASP.NET and Sharepoint project so not sure what gives.

Forcing the Unit Test project to use the linked web.config file from the ASP.NET project

When I unit test the Business Layer project associated with my WinForms app, in my UnitTest project I include an app.config that's linked from the Winforms project. This way, if I change anything in the EXE's app.config, it'll be reflected in my Unit Test.
I'd like to do the same with my ASP.NET projects, however, they use web.config, thus I can't simply link it to the UnitTest project.
So short of having a pre-build step that copies the web.config into UnitTest project's app.config, how can I get around this issue?

Using Nunit without creating separate project

I am creating one web application which has structure as shown in following image:
I have all entities and datastore files in the App_Code folder. i.e no separate project layer is created. I want to use Nunit for unit testing. But as for NUnit testing I need projectname.dll and my web application will not create dll I don't know how to test my methods.
If I create a separate project for unit tests, I can not reference datastore and other files in that project. Kindly suggest how can I use Nunit in this case.
Please, check this: Unit Testing ASP.net Web Site Project code stored in App_Code.
You can also create another project, just for unit testing purposes. When you create it, just include all the required files (in App_Code) As a Link. That way you will be creating shortcuts, so if you modify one file in your project, the other project you created will reflect the changes.

How to use NUnit GUI with a C#/ASP.NET website?

I have a C#/ASP.NET website that has some code (*.cs) files in the App_Code directory. I would like to test them using NUnit. I have written a test file with the proper [TestFixture] and [Test] annotations and have put it here: App_Code/Test/TestClassName.cs.
I load up the NUnit GUI to run it but it wants me to select a .exe or .dll file. There is none in the bin folder of my project. My project does successfully run and is built and everything, but still no exe or dll file. How can I get the NUnit Gui to just run the test in that class?
I don't recommend putting test code in the same package you'll be deploying to production.
You may want to move the test classes to a library project, something like Business.UnitTest(there may be a built in way to create an nUnit specific project, if there is, use that). Then move the business classes that are in your App_Code directory into another project called Business. Have Business.UnitTest reference the Business library. That should get nUnit to run(I don't believe that nUnit runs against websites, only libraries, but I'm not 100% sure).
For your website add a reference to the business library you just created so your production code can access the business objects in the business library. You may have to work out some namespace issues, but that shouldn't be too bad.
The trick with .NET Website projects is that the code files are not normally compiled up front, they are compiled on execution of the respective pages. This presents a challenge where testing is concerned, since as you mentioned NUnit wants to run a .exe or .dll for testing.
One way to deal with the issue is to convert the website project to a web application; they sound similar, but work in different ways. In contrast to a website not requiring up-front compilation, a web application requires it. So you would have one or more projects that compile to assemblies (.dll) or executables (.exe). NUnit could then hook into those to run the tests.
To make this work, you would want to separate testable code into another project; your front-end web application can refer to this other project to make use of the code within. Ideally, the front-end would be a thin layer of logic and user interaction, and the real work can be sent to the second project. Therefore, the second project is what you will want to test.
You'll want to have one more project to contain the tests - general wisdom is to not have your tests in the same project as the code being tested. This project refers to the project being tested, and to NUnit, and contains the tests themselves. This assembly is what you would direct NUnit to run for testing.
First, you want to create a new project for your tests. If you happen to have any internal classes or attributes, you should use InternalsVisibleToAttribute in order to be able to test these from within your testing project, outside your "real" project.
This attribute is suitable for the entire assembly, so I recommend putting it into your Assembly.info file of your "real" assembly.
From within your tests project, add a reference to your "real" assembly.
Make sure to exactly know the path to your binary (assembly.dll);
Open your TestsProjectAssembly.dll from within your NUnit GUI, makeing sure you are browsing to the right folder;
You might also want NUnit GUI to reload your assembly on each tests run (there is an option for doing so in the options properties);
Run your tests.
Absolutely make sure your path or browsable folder is the one in which your testing project is generated.

Categories

Resources