xunit

Mocking MediatR 3 with Moq

陌路散爱 提交于 2021-02-06 15:24:18
问题 We've recently started using MediatR to allow us to de-clutter controller actions as we re-factor a large customer facing portal and convert it all to C#. As part of this we are increasing our unit test coverage as well, but I've hit a problem when trying to mock MediatR itself. The command does a bunch of stuff to initiate a process and part of this is sending a notification. The notification itself is dealt with by its own handler and therefore would be subject to its own unit test so I

Mocking MediatR 3 with Moq

心已入冬 提交于 2021-02-06 15:19:00
问题 We've recently started using MediatR to allow us to de-clutter controller actions as we re-factor a large customer facing portal and convert it all to C#. As part of this we are increasing our unit test coverage as well, but I've hit a problem when trying to mock MediatR itself. The command does a bunch of stuff to initiate a process and part of this is sending a notification. The notification itself is dealt with by its own handler and therefore would be subject to its own unit test so I

Replacing a entity collection in Entity Framework Core causes DbContext to fetch the new values when not saved to db. How to reload the collection?

99封情书 提交于 2021-02-04 08:36:08
问题 What I can't understand fully is exemplified in a unit test below. Basically I want to know why DbContext overrides collection values currently stored in the database even if Reload() and a new load for the collection has been called. If I try to call dbContext.Entry(test.TestChildren).Reload(); I get the exception: System.InvalidOperationException: 'The entity type 'List' was not found. Ensure that the entity type has been added to the model.' I have also loaded TestChildren explicitly via

Visual Studio 2019 suddenly requires nuget Microsoft.NET.Test.Sdk to run xUnit unit-tests

拜拜、爱过 提交于 2021-01-29 07:19:04
问题 I'm using xUnit for implementing tests. I used to install the xUnit Visual Studio runner, xunit.runner.visualstudio nuget package to run tests using the VS GUI. Below is are xUnit-related branches of the dependency tree in VS. You can clearly see the xUnit VS runner has no dependencies: Visual Studio is suddenly asking me to install the Microsoft.NET.Test.Sdk nuget in order to run tests in the GUI. Anyone knows why? Note: I have been using VS 2019 for over a year frequently updating asap. The

When setting up a custom AutoDataAttribute for auto mocking, what's the proper syntax to tell AutoFixture to ignore all recursive structures?

吃可爱长大的小学妹 提交于 2021-01-29 03:09:46
问题 I have xUnit/Moq/AutoFixture successfully working together so that I am auto mocking objects via test method input parameters. I created a custom [AutoMoqData] attribute which I use on every test. Here's the code for the attribute: using System.Linq; using AutoFixture; using AutoFixture.AutoMoq; using AutoFixture.Xunit2; namespace Shared.TestResources.AutoFixture { public class AutoMoqDataAttribute : AutoDataAttribute { public AutoMoqDataAttribute() : base(() => new Fixture().Customize(new

xunit “could not load type” error

谁都会走 提交于 2021-01-28 19:44:17
问题 I inherited a project and the tests ran fine. Today, I created a new class, and xunit errors out when I try to use this class in a test. Both the test project and the app are targeting .NET Framework 4.6.1. I do clean and rebuild on the both the solution and the test project, but I still get the error. error: Could not load type 'MyNewClass' from assembly 'MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. project.json: "buildOptions": { "warningsAsErrors": true }, "testRunner":

How to mock Db connection in xunit?

送分小仙女□ 提交于 2021-01-28 13:45:05
问题 I am writing unit test cases in xUnit. I am writing unit test case for opening database connection. I am writing negative case when database is down. Below is the real code where I am making connection to database. public IDbConnection CreateDirectDb2Connection(int attempt = 0) { try { var conn = new DB2Connection(BuildDB2ConnectionString()); conn.Open(); return conn; } catch (Exception ex) { logService.Debug($"Failed to create and open a connection, attempt {attempt + 1}/3, error: {ex}"); if

Azure Pipelines counts xUnit InlineData as one test instead of many

ⅰ亾dé卋堺 提交于 2021-01-28 09:11:02
问题 In our Azure Pipelines pipeline we have .NET Core xUnit test methods that take an InlineData parameter. The test runner runs all test methods and correctly reports in its console output each InlineData instance as a test run. However, Azure Pipelines reports a lower test count because it counts the [Theory] tests as one test (instead of one test for every instance of InlineData). How can I make Azure Pipelines report all instances of InlineData as tests? As an example, here's a job output.

Visual Studio 2019 Test Explorer not working well with xUnit

梦想的初衷 提交于 2021-01-27 22:13:30
问题 I've just installed Visual Studio 2019 Professional and I noticed some misbehavior if I compare it to the VS 2017 which I used to work with. The Test Explorer window In the VS 2017 when I open a project, that already had been opened previously, I see all the tests in the Test Explorer. In the VS 2019 the window is empty until I rebuild the solution. If I rebuild the solution and all the xUnit tests are discovered I cannot click on any single test. I can click on all the folders in the

ASP.NET Core Testing - get NullReferenceException when initializing InMemory SQLite dbcontext in fixture

£可爱£侵袭症+ 提交于 2021-01-26 03:47:06
问题 I have a test fixture in which I initialize my SQLite in-memory dbcontext, shown below: public static MYAPPDBContext Create() { var options = new DbContextOptionsBuilder<MYAPPDBContext>() .UseSqlite("DataSource=:memory:") .Options; var context = new MYAPPDBContext(options); context.Database.OpenConnection(); // this is where exception is thrown context.Database.EnsureCreated(); return context; } When I call the Create() method, I get the following NullReferenceException: System