-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoFixtureTests.cs
59 lines (48 loc) · 1.92 KB
/
AutoFixtureTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using AutoFixture;
using AutoFixture.Kernel;
using AutoFixture.Xunit2;
using AutoFixtureKataStarter.Model;
using AutoFixtureKataStarter.Services;
using Moq;
using Xunit;
namespace AutoFixtureKataStarter
{
/// <summary>
/// These are provided to show some examples of AutoFixture usage
/// </summary>
public class AutoFixtureTests
{
private IFixture _fixture;
public AutoFixtureTests()
{
_fixture = new Fixture();
}
[Fact]
public void ExplicitMockOrInstanceUsage()
{
var mockLogger = new Mock<IFileLogger>();
_fixture.Register(() => mockLogger.Object); // add the mock to this fixture
var orderService = _fixture.Create<OrderService>(); // will use the mockLogger to create
var order = _fixture.Build<Order>()
.With(o => o.Id, 0)
.With(o => o.Customer, _fixture.Build<Customer>()
.Without(c => c.OrderHistory).Create) // AutoFixture doesn't support loops in object hierarchies
.Create();
// Act
orderService.PlaceOrder(order);
// Assert
mockLogger.Verify(l => l.Log(It.IsAny<string>()), "OrderService created.");
}
[Theory, AutoMoqData]
public void LogMessage(Order order, [Frozen] Mock<IFileLogger> mockLogger, OrderService sut)
{
// This test does the same as the previous test with much less code
// This is one of the key values of AutoFixture. Try to use this style in your tests for this exercise.
order.Id = 0; // reset this to 0 so our test passes; otherwise it will have a random int value
// Act
sut.PlaceOrder(order); // sut = 'system under test'
// Assert
mockLogger.Verify(l => l.Log(It.IsAny<string>()), "OrderService created.");
}
}
}