TUnit.Mocks - Feedback #4981
Replies: 13 comments 35 replies
-
|
Hi. Just personal preference, but the syntax for mocks is long (like in the Moq). I prefer NSubtitute. It's more natural. // TUnit Mocks
// Arrange — create a mock
var mock = Mock.Of<IGreeter>();
// Configure — set up a return value
mock.Setup.Greet(Arg.Any<string>()).Returns("Hello!");
// Act — use the mock object
IGreeter greeter = mock.Object;
var result = greeter.Greet("Alice");
// Assert — verify the result and the call
await Assert.That(result).IsEqualTo("Hello!");
mock.Verify.Greet("Alice").WasCalled(Times.Once);
//vs NSubstitute
//Create:
var calculator = Substitute.For<ICalculator>();
//Set a return value:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(3, calculator.Add(1, 2));
//Check received calls:
calculator.Received().Add(1, Arg.Any<int>()); |
Beta Was this translation helpful? Give feedback.
-
|
Hello! I'm currently planning to write tests using TUnit and looking for an AOT-friendly, source-generator-based mocking framework for a reflection-free testing stack. I have a few questions:
var mock = Mock.Of<ITest>();
public interface ITest
{
void Do(out ReadOnlySpan<byte> buffer);
}
|
Beta Was this translation helpful? Give feedback.
-
|
As a user of Moq, I always disliked, having to specify those For example instead of this... var equalityComparer = Mock.Of<IEqualityComparer<int>>();
var equals = equalityComparer.Equals_(Arg.Any<int>(), Arg.Is<int>(b => b > 0)).Returns((a, b) => a == b);
// ... some testing code ...
await Assert.That(equals).WasCalled(Times.Once);... this might be an alternative: var equalityComparer = Mock.Of<IEqualityComparer<int>>();
var equals = equalityComparer.SetupEquals((a, b) => a == b, if: (_, b) => b > 0);
// ... some testing code ...
await Assert.That(equals).WasCalled(Times.Once);It is not necessary the best way to do it and it will need some refinement, but I think it is already better than the classical syntax.
|
Beta Was this translation helpful? Give feedback.
-
|
I just found this and would like to use it, but I have a question. service
.Method(Args.Any<bool>())
.Returns(
Task.FromException(new Exception()),
Task.CompletedTask,
Task.FromException(new Exception()),
Task.CompletedTask,
Task.FromException(new Exception()));How can I do the same thing with TUnit.Mocks? |
Beta Was this translation helpful? Give feedback.
-
|
I really like TUnit.Mocks. Using "It"/"Arg" as filter is a great idea, love it! There is something I wish that'd be possible: Let's assume that I have the following interface: interface IMyService
{
int DoSomething(int a, int b, int c, int d, int e, int f);
}The way to mock this is: var myServiceMock = IMyService.Mock();
myServiceMock.DoSomething(Any(), Any(), Any(), Any(), Any(), Any())
.Returns(1);And validation would look like this: myServiceMock.DoSomething(Any(), Any(), Any(), Any(), Any(), Any())
.WasCalled(Times.Once);Do you notice the many "Any()"? I wish that there would be a way (maybe synthetic overload) that would allow: myServiceMock.DoSomething(AnyAll())
.WasCalled(Times.Once);
// or
myServiceMock.All.DoSomething
.WasCalled(Times.Once); |
Beta Was this translation helpful? Give feedback.
-
|
It cannot yet mock methods with generics, can it? This does not work: Works ok with NSubstitute. |
Beta Was this translation helpful? Give feedback.
-
|
How do I return the value that was passed to a method? This does not work: |
Beta Was this translation helpful? Give feedback.
-
|
Also I cannot mock IConfiguration. This does not compile: with NSubstitute this works ok:
|
Beta Was this translation helpful? Give feedback.
-
|
any way to globally move to strict mock instead of writing the same thing in each test. I have come across many scenarios where the loose mocking made the tests flaky. with strict mocks I get those failures much earlier, but with agents writing most of my tests, there's a real possibility that I miss the strict mocking in review and tests again become flaky. |
Beta Was this translation helpful? Give feedback.
-
|
How would I match in case method has params modifier? This does not compile: public interface IGreeter
{
string Greet(params object[] keyValues);
}
public class GreeterTests
{
[Test]
public async Task Greet_Returns_Configured_Value()
{
var mock = IGreeter.Mock();
// ???
mock.Greet(Is(1)).Returns("Hello!");
IGreeter greeter = mock;
var result = greeter.Greet(1);
await Assert.That(result).IsEqualTo("Hello!");
}
} |
Beta Was this translation helpful? Give feedback.
-
|
The https://tunit.dev/docs/writing-tests/mocking/verification#tunit-assertion-integration does not seem to be working. Example: public interface IGreeter
{
string Greet(string name);
}
public class GreeterTests
{
[Test]
public async Task Greet_Returns_Configured_Value()
{
var mock = IGreeter.Mock();
mock.Greet(Any()).Returns("Hello!");
IGreeter greeter = mock;
var result = greeter.Greet("Alice");
mock.Greet(Any()).WasCalled(Times.Once);
// this does not compile
//await Assert.That(mock.Greet(Any())).WasCalled(Times.Once);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
TUnit.Mocks is only for .net 10? On the documentation say it require C#14 but the Nuget show it can be used on .net 8. |
Beta Was this translation helpful? Give feedback.
-
|
I'm trying to setup a multi-interface mock with behavior. How can I mock behavior on the second interface? var dbContext = Mock.Of<DbContext, IInfrastructure<IServiceProvider>>();
dbContext.Instance.Returns(...); // Instance is a property of IInfrastructure
// CS1061: 'Mock<DbContext>' does not contain a definition for 'Instance'...If I try to cast it to If I try to cast it to I'm comming from NSubstitute: dbContext = Substitute.For<DbContext, IInfrastructure<IServiceProvider>>();
((IInfrastructure<IServiceProvider>)dbContext).Instance.Returns(serviceProvider); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey all - So TUnit.Mocks is now available as a beta package. You should be able to install it from IDEs once you've checked the allow pre releases checkbox.
I would appreciate people to try it out and if you have any feedback, good or bad, please let me know! :)
Beta Was this translation helpful? Give feedback.
All reactions