How to attach a message to RSpec check?

For RSpec 3+: The message could be customized as a string or using a proc(check the reference). expect(1).to eq(2), ‘one is not two!’ Customized message RSpec tries to provide useful failure messages, but for cases in which you want more specific information, you can define your own message right in the example. This works for … Read more

How to verify ILogger.Log extension method has been called using Moq?

You can’t mock extension methods. Instead of mocking logger.LogError(…) You need to mock the interface method void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter); LogError actually calls that interface method like this logger.Log(LogLevel.Error, 0, new FormattedLogValues(message, args), null, (state, ex) => state.ToString()); So you need to mock _loggerMock.Verify(logger => logger.Log(It.Is(LogLevel.Error), … Read more

how to debug with xUnit?

In VS2015 and later, install the xunit.runner.visualstudio NuGet package. Then debugging is as easy as right-clicking on the test in the test explorer window. (Test–>Windows–>TestExplorer if you can’t see it). You can also right-click anywhere in the code of the test and Run Test and Debug Test will be in the context menu. If your … Read more

xUnit Equivalent of MSTest’s Assert.Inconclusive

Best thing one can do until something is implemented in the library is to use Xunit.SkippableFact: [SkippableFact] public void SomeTest() { var canRunTest = CheckSomething(); Skip.IfNot(canRunTest); // Normal test code } This will at least make it show up as a yellow ignored test case in the list. Credit goes to https://stackoverflow.com/a/35871507/537842

How to implement XUnit descriptive Assert message?

Use the suggestions provided at the link. Like fluent assertions or create your own assertion that wraps the Assert.True or Assert.False which were left with their message overloads. It was mentioned further down Quote You can provide messages to Assert.True and .False. If you simply cannot live without messages (and refuse to use a different … Read more

xUnit or NUnit? What advantages and disadvantages of each other? [duplicate]

I work with NUnit only. It is pretty good for me. It is integrated into different add-ons for Visual Studio like Resharper, it supports in TeamCity and it has stand-alone test launcher And looks like NUnit become an industry standard for unit testing. And we haven’t had any major issues with Moq or Rhino.Mock. As … Read more

Pass array of string to xunit test method

Use params before the method’s string[] argument, and then you won’t need to initialize a string[] in InlineData attribute, rather you could use a variable number of string literals, for which the compiler doesn’t complain one bit: [Theory] [InlineData(“2000-01-02”, “2000-02-01”)] public void TestSynchronizeMissionStaffing_PeriodNoMatch(params string[] dateStrings)

Is it possible to use Dependency Injection with xUnit?

Yes it’s possible with Xunit.DependencyInjection Install-Package Xunit.DependencyInjection and you can inject your services [assembly: TestFramework(“Your.Test.Project.Startup”, “AssemblyName”)] namespace Your.Test.Project { public class Startup : DependencyInjectionTestFramework { public Startup(IMessageSink messageSink) : base(messageSink) { } protected override void ConfigureServices(IServiceCollection services) { services.AddTransient<IDependency, DependencyClass>(); } } } https://github.com/pengweiqhca/Xunit.DependencyInjection