Skip to content

Commit e4e7d32

Browse files
authored
Merge pull request #33 from roodfluweel/codex/convert-mstest-to-xunit-with-shouldly-and-nsubstitute
Convert tests to xUnit v3 with Shouldly and NSubstitute
2 parents 3106416 + bb02b93 commit e4e7d32

File tree

7 files changed

+148
-186
lines changed

7 files changed

+148
-186
lines changed

PayNLSdk.Tests/Api/Alliance/AddInvoiceRequestTests.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using Shouldly;
2+
using Xunit;
53

64
namespace PayNLSdk.Tests.Api.Alliance
75
{
8-
[TestClass]
96
public class AddInvoiceRequestTests
107
{
11-
private PAYNLSDK.API.Alliance.AddInvoice.Request _sut;
8+
private readonly PAYNLSDK.API.Alliance.AddInvoice.Request _sut;
129

13-
[TestInitialize]
14-
public void TestInitialize()
10+
public AddInvoiceRequestTests()
1511
{
1612
_sut = new PAYNLSDK.API.Alliance.AddInvoice.Request(
1713
"Dummy",
1814
"Dummy",
1915
"Dummy",
2016
"Dummy",
2117
12345
22-
);
18+
);
2319
}
24-
25-
[TestMethod]
20+
21+
[Fact]
2622
public void GetParameters_ReturnMandatoryProperties_OnCalled()
2723
{
2824
// Arrange
29-
25+
3026
// Act
3127
var parameters = _sut.GetParameters();
3228

3329
// Assert
34-
Assert.IsNotNull(parameters["serviceId"]);
35-
Assert.IsNotNull(parameters["merchantId"]);
36-
Assert.IsNotNull(parameters["invoiceId"]);
37-
Assert.IsNotNull(parameters["amount"]);
38-
Assert.IsNotNull(parameters["description"]);
30+
parameters["serviceId"].ShouldNotBeNull();
31+
parameters["merchantId"].ShouldNotBeNull();
32+
parameters["invoiceId"].ShouldNotBeNull();
33+
parameters["amount"].ShouldNotBeNull();
34+
parameters["description"].ShouldNotBeNull();
3935
}
4036

4137
#region Optional parameters
42-
[TestMethod]
38+
[Fact]
4339
public void MakeYesterday_internalPropertySet_True()
4440
{
4541
// Arrange
@@ -48,10 +44,10 @@ public void MakeYesterday_internalPropertySet_True()
4844
_sut.MakeYesterday = true;
4945

5046
// Assert
51-
Assert.AreEqual("true", _sut.GetParameters()["makeYesterday"]);
47+
_sut.GetParameters()["makeYesterday"].ShouldBe("true");
5248
}
5349

54-
[TestMethod]
50+
[Fact]
5551
public void MakeYesterday_propertyNotAvailableInInParameters_PropertyIsNotSet()
5652
{
5753
// Arrange
@@ -61,10 +57,10 @@ public void MakeYesterday_propertyNotAvailableInInParameters_PropertyIsNotSet()
6157
var parameter = _sut.GetParameters()["makeYesterday"];
6258

6359
// Assert
64-
Assert.IsNull(parameter);
60+
parameter.ShouldBeNull();
6561
}
6662

67-
[TestMethod]
63+
[Fact]
6864
public void InvoiceUrl_internalPropertySet_True()
6965
{
7066
// Arrange
@@ -74,20 +70,19 @@ public void InvoiceUrl_internalPropertySet_True()
7470
_sut.InvoiceUrl = httpUrlToInvoice;
7571

7672
// Assert
77-
Assert.AreEqual(httpUrlToInvoice, _sut.GetParameters()["invoiceUrl"]);
73+
_sut.GetParameters()["invoiceUrl"].ShouldBe(httpUrlToInvoice);
7874
}
7975

80-
[TestMethod]
76+
[Fact]
8177
public void InvoiceUrl_propertyNotAvailableInInParameters_PropertyIsNotSet()
8278
{
8379
// Arrange
8480
//_sut.MakeYesterday = null;
8581

8682
// Act
8783
var parameter = _sut.GetParameters()["invoiceUrl"];
88-
8984
// Assert
90-
Assert.IsNull(parameter);
85+
parameter.ShouldBeNull();
9186
}
9287
#endregion
9388
}

PayNLSdk.Tests/Api/Statistics/GetStatsTests.cs

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
using System;
2-
using Microsoft.VisualStudio.TestTools.UnitTesting;
3-
using Moq;
4-
using PayNLSdk.API.Statistics.GetManagement;
1+
using System;
52
using System.Collections.Generic;
63
using System.Collections.Specialized;
74
using System.Diagnostics.CodeAnalysis;
85
using System.Linq;
6+
using NSubstitute;
7+
using PayNLSdk.API.Statistics.GetManagement;
8+
using Shouldly;
9+
using Xunit;
910

1011
namespace PayNLSdk.Tests.Api.Statistics
1112
{
12-
[TestClass]
1313
[SuppressMessage("ReSharper", "RedundantNameQualifier", Justification = "We need to be sure the correct object is called in the tests")]
1414
public class GetStatsRequestTests
1515
{
16-
private PayNLSdk.API.Statistics.GetManagement.Request _sut;
16+
private readonly PayNLSdk.API.Statistics.GetManagement.Request _sut;
1717

18-
[TestInitialize]
19-
public void TestInitialize()
18+
public GetStatsRequestTests()
2019
{
2120
_sut = new PayNLSdk.API.Statistics.GetManagement.Request();
2221
}
2322

24-
[TestMethod]
23+
[Fact]
2524
public void Ctor_FilterPropertyNotTempty_Always()
2625
{
2726
// Arrange
@@ -30,11 +29,11 @@ public void Ctor_FilterPropertyNotTempty_Always()
3029
// Act
3130

3231
// Assert
33-
Assert.IsNotNull(_sut.Filters);
34-
Assert.AreEqual(0, _sut.Filters.Count);
32+
_sut.Filters.ShouldNotBeNull();
33+
_sut.Filters.Count.ShouldBe(0);
3534
}
3635

37-
[TestMethod]
36+
[Fact]
3837
public void Ctor_GroupByPropertyNotTempty_Always()
3938
{
4039
// Arrange
@@ -43,11 +42,11 @@ public void Ctor_GroupByPropertyNotTempty_Always()
4342
// Act
4443

4544
// Assert
46-
Assert.IsNotNull(_sut.GroupByFieldNames);
47-
Assert.AreEqual(0, _sut.GroupByFieldNames.Count);
45+
_sut.GroupByFieldNames.ShouldNotBeNull();
46+
_sut.GroupByFieldNames.Count.ShouldBe(0);
4847
}
4948

50-
[TestMethod]
49+
[Fact]
5150
public void GetParameters_ContainsGroupBy_IfSortByFieldNamesPropertyIsUsed()
5251
{
5352
// Arrange
@@ -57,11 +56,10 @@ public void GetParameters_ContainsGroupBy_IfSortByFieldNamesPropertyIsUsed()
5756
var result = _sut.GetParameters();
5857

5958
// Assert
60-
Assert.AreEqual("ABC", result.Get("groupBy[0]"));
61-
59+
result.Get("groupBy[0]").ShouldBe("ABC");
6260
}
6361

64-
[TestMethod]
62+
[Fact]
6563
public void GetParameters_ContainsFilters_MultipleFiltersAdded()
6664
{
6765
// Arrange
@@ -72,70 +70,70 @@ public void GetParameters_ContainsFilters_MultipleFiltersAdded()
7270
var result = _sut.GetParameters();
7371

7472
// Assert
75-
Assert.IsTrue(GetWithPartialKey(result, "filterType[").Contains("KEY1"));
76-
Assert.IsTrue(GetWithPartialKey(result, "filterType[").Contains("KEY2"));
77-
Assert.IsTrue(GetWithPartialKey(result, "filterValue[").Contains("VAL1"));
78-
Assert.IsTrue(GetWithPartialKey(result, "filterValue[").Contains("VAL2"));
73+
GetWithPartialKey(result, "filterType[").ShouldContain("KEY1");
74+
GetWithPartialKey(result, "filterType[").ShouldContain("KEY2");
75+
GetWithPartialKey(result, "filterValue[").ShouldContain("VAL1");
76+
GetWithPartialKey(result, "filterValue[").ShouldContain("VAL2");
7977
}
8078

81-
[TestMethod]
79+
[Fact]
8280
public void Create_CorrectStartEndDate_LastWeek()
8381
{
8482
// Arrange
85-
var dateTime = new Mock<IDateTime>();
86-
dateTime.SetupGet(p => p.Now).Returns(new DateTime(2018, 12, 11));
83+
var dateTime = Substitute.For<IDateTime>();
84+
dateTime.Now.Returns(new DateTime(2018, 12, 11));
8785

8886
// Act
89-
var result = Request.Create(dateTime.Object, Request.StatsPeriod.LastWeek);
87+
var result = Request.Create(dateTime, Request.StatsPeriod.LastWeek);
9088

9189
// Assert
92-
Assert.AreEqual(new DateTime(2018,12,3), result.StartDate);
93-
Assert.AreEqual(new DateTime(2018,12,9), result.EndDate);
90+
result.StartDate.ShouldBe(new DateTime(2018, 12, 3));
91+
result.EndDate.ShouldBe(new DateTime(2018, 12, 9));
9492
}
9593

96-
[TestMethod]
94+
[Fact]
9795
public void Create_CorrectStartEndDate_LastMonth()
9896
{
9997
// Arrange
100-
var dateTime = new Mock<IDateTime>();
101-
dateTime.SetupGet(p => p.Now).Returns(new DateTime(2018, 12, 11));
98+
var dateTime = Substitute.For<IDateTime>();
99+
dateTime.Now.Returns(new DateTime(2018, 12, 11));
102100

103101
// Act
104-
var result = Request.Create(dateTime.Object, Request.StatsPeriod.LastMonth);
102+
var result = Request.Create(dateTime, Request.StatsPeriod.LastMonth);
105103

106104
// Assert
107-
Assert.AreEqual(new DateTime(2018, 11, 1), result.StartDate);
108-
Assert.AreEqual(new DateTime(2018, 11, 30), result.EndDate);
105+
result.StartDate.ShouldBe(new DateTime(2018, 11, 1));
106+
result.EndDate.ShouldBe(new DateTime(2018, 11, 30));
109107
}
110108

111-
[TestMethod]
109+
[Fact]
112110
public void Create_CorrectStartEndDate_ThisWeek()
113111
{
114112
// Arrange
115-
var dateTime = new Mock<IDateTime>();
116-
dateTime.SetupGet(p => p.Now).Returns(new DateTime(2018, 12, 11));
113+
var dateTime = Substitute.For<IDateTime>();
114+
dateTime.Now.Returns(new DateTime(2018, 12, 11));
117115

118116
// Act
119-
var result = Request.Create(dateTime.Object, Request.StatsPeriod.ThisWeek);
117+
var result = Request.Create(dateTime, Request.StatsPeriod.ThisWeek);
120118

121119
// Assert
122-
Assert.AreEqual(new DateTime(2018, 12, 10), result.StartDate);
123-
Assert.AreEqual(new DateTime(2018, 12, 16), result.EndDate);
120+
result.StartDate.ShouldBe(new DateTime(2018, 12, 10));
121+
result.EndDate.ShouldBe(new DateTime(2018, 12, 16));
124122
}
125123

126-
[TestMethod]
124+
[Fact]
127125
public void Create_CorrectStartEndDate_ThisMonth()
128126
{
129127
// Arrange
130-
var dateTime = new Mock<IDateTime>();
131-
dateTime.SetupGet(p => p.Now).Returns(new DateTime(2018, 12, 11));
128+
var dateTime = Substitute.For<IDateTime>();
129+
dateTime.Now.Returns(new DateTime(2018, 12, 11));
132130

133131
// Act
134-
var result = Request.Create(dateTime.Object, Request.StatsPeriod.ThisMonth);
132+
var result = Request.Create(dateTime, Request.StatsPeriod.ThisMonth);
135133

136134
// Assert
137-
Assert.AreEqual(new DateTime(2018, 12, 1), result.StartDate);
138-
Assert.AreEqual(new DateTime(2018, 12, 11), result.EndDate);
135+
result.StartDate.ShouldBe(new DateTime(2018, 12, 1));
136+
result.EndDate.ShouldBe(new DateTime(2018, 12, 11));
139137
}
140138

141139
/// <summary>

PayNLSdk.Tests/Api/Transaction/TransactionRefundTests.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using Shouldly;
2+
using Xunit;
53

64
namespace PayNLSdk.Tests.Api.Transaction
75
{
8-
[TestClass]
96
public class TransactionRefundTests
107
{
11-
[TestMethod]
8+
[Fact]
129
public void Request_AmountInCents_PassedInAsDecimal()
1310
{
1411
// Arrange
@@ -22,10 +19,10 @@ public void Request_AmountInCents_PassedInAsDecimal()
2219
var result = sut.GetParameters();
2320

2421
// Assert
25-
Assert.AreEqual("350", result["amount"]);
22+
result["amount"].ShouldBe("350");
2623
}
2724

28-
[TestMethod]
25+
[Fact]
2926
public void Request_NoAmountSupplied_NoParameterWithAmount()
3027
{
3128
// Arrange
@@ -39,9 +36,7 @@ public void Request_NoAmountSupplied_NoParameterWithAmount()
3936
var result = sut.GetParameters();
4037

4138
// Assert
42-
Assert.IsNull(result["amount"]);
39+
result["amount"].ShouldBeNull();
4340
}
44-
45-
4641
}
4742
}

0 commit comments

Comments
 (0)