Net Core: Execute All Dependency Injection in Xunit Test for AppService, Repository, etc

You are mixing unit test with integration test. TestServer is for integration test and if you want to reuse Startup class to avoid register dependencies again, you should use HttpClient and make HTTP call to controller and action that use IDepartmentAppService. If you want do unit test, you need to setup DI and register all … Read more

how do I mock sqlconnection or should I refactor the code?

SqlBulkWriter class is tightly coupled to implementation concerns which make it difficult to test the class in isolation. Knowing the connection string is not really a concern of that class and can be delegated out to another service. Something like public interface IDbConnectionFactory { IDbConnection CreateConnection(); } and its implementation for your class would look … Read more

xUnit : Assert two List are equal?

2021-Apr-01 update I recommend using FluentAssertions. It has a vast IntelliSense-friendly assertion library for many use cases including collections collection.Should().Equal(1, 2, 5, 8); collection.Should().NotEqual(8, 2, 3, 5); collection.Should().BeEquivalentTo(8, 2, 1, 5); Original answer xUnit.Net recognizes collections so you just need to do Assert.Equal(expected, actual); // Order is important You can see other available collection assertions … Read more

Assert an Exception using XUnit

The Assert.Throws expression will catch the exception and assert the type. You are however calling the method under test outside of the assert expression and thus failing the test case. [Fact] public void ProfileRepository_GetSettingsForUserIDWithInvalidArguments_ThrowsArgumentException() { //arrange ProfileRepository profiles = new ProfileRepository(); // act & assert Assert.Throws<ArgumentException>(() => profiles.GetSettingsForUserID(“”)); } If bent on following AAA you … Read more

Mock Verify() Invocation

I’m going to go a step further than Yoshi’s comment. The Performed invocations message tells you the method was called but not with the parameters that you were verifying. My guess based on the messages is that there’s something wrong with the first parameter. You would need to post the test for me to be … Read more

Visual Studio 2015 or 2017 does not discover unit tests

To my surprise, clearing temp files located in the %TEMP% directory resolved the issue for me. Note: This path is generally at C:\Users\(yourusername)\AppData\Local\Temp As @Warren-P included, you can navigate to the temp folder by putting in %temp% in Start Menu, or launch “File Explorer” and enter %temp% in the address bar.

What is the JUnit XML format specification that Hudson supports?

I did a similar thing a few months ago, and it turned out this simple format was enough for Hudson to accept it as a test protocol: <testsuite tests=”3″> <testcase classname=”foo1″ name=”ASuccessfulTest”/> <testcase classname=”foo2″ name=”AnotherSuccessfulTest”/> <testcase classname=”foo3″ name=”AFailingTest”> <failure type=”NotEnoughFoo”> details about failure </failure> </testcase> </testsuite> This question has answers with more details: Spec. for … Read more

Pass complex parameters to [Theory]

There are many xxxxData attributes in XUnit. Check out for example the MemberData attribute. You can implement a property that returns IEnumerable<object[]>. Each object[] that this method generates will be then “unpacked” as a parameters for a single call to your [Theory] method. See i.e. these examples from here Here are some examples, just for … Read more