TUnit.Mocks - Feedback #4981
Replies: 4 comments 16 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.
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