Skip to content

Commit fc8979c

Browse files
Remove Moq from unit tests (#127)
Swapped Moq for NSubstitute
1 parent 7046cd9 commit fc8979c

File tree

102 files changed

+2174
-2219
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2174
-2219
lines changed

src/unit-tests/graphql-aspnet-subscriptions-tests/Configuration/ConfigurationSetupTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace GraphQL.AspNet.Tests.Configuration
2525
using GraphQL.AspNet.Tests.Configuration.ConfigurationTestData;
2626
using Microsoft.Extensions.DependencyInjection;
2727
using Microsoft.Extensions.Hosting;
28-
using Moq;
28+
using NSubstitute;
2929
using NUnit.Framework;
3030

3131
[TestFixture]
@@ -182,8 +182,8 @@ public void AddSubscriptionPublishing_RegistrationChecks()
182182
// both of which are not "publishing specific"
183183
//
184184
// register this mock one to remove the dependency
185-
var externalPublisher = new Mock<ISubscriptionEventPublisher>();
186-
serviceCollection.AddSingleton(externalPublisher.Object);
185+
var externalPublisher = Substitute.For<ISubscriptionEventPublisher>();
186+
serviceCollection.AddSingleton(externalPublisher);
187187

188188
var returned = serviceCollection.AddGraphQL(options =>
189189
{

src/unit-tests/graphql-aspnet-subscriptions-tests/Controllers/ControllerExtensionTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task PublishSubEvent_PublishesEventWithCorrectData()
4040
var resolutionContext = fieldContextBuilder.CreateResolutionContext();
4141

4242
var controller = new InvokableController();
43-
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod.Object, resolutionContext);
43+
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod, resolutionContext);
4444

4545
// ensure the method executed completely
4646
Assert.IsNotNull(result);
@@ -81,7 +81,7 @@ public void PublishSubEvent_NoDataThrowsException()
8181

8282
Assert.ThrowsAsync<ArgumentNullException>(async () =>
8383
{
84-
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod.Object, resolutionContext);
84+
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod, resolutionContext);
8585
});
8686
}
8787

@@ -103,7 +103,7 @@ public async Task PublishSubEvent_ExistingEventCollectionisAppendedTo()
103103
resolutionContext.Session.Items.TryAdd(SubscriptionConstants.ContextDataKeys.RAISED_EVENTS_COLLECTION, eventCollection);
104104

105105
var controller = new InvokableController();
106-
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod.Object, resolutionContext);
106+
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod, resolutionContext);
107107

108108
// ensure the method executed completely
109109
Assert.IsNotNull(result);
@@ -136,7 +136,7 @@ public void PublishSubEvent_UnusableListForSubscriptionEvents_ThrowsException()
136136
var controller = new InvokableController();
137137
Assert.ThrowsAsync<GraphExecutionException>(async () =>
138138
{
139-
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod.Object, resolutionContext);
139+
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod, resolutionContext);
140140
});
141141
}
142142

@@ -158,7 +158,7 @@ public void PublishSubEvent_NoEventNameFailsTheResolver_BubblesExceptionUp()
158158

159159
Assert.ThrowsAsync<ArgumentException>(async () =>
160160
{
161-
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod.Object, resolutionContext);
161+
var result = await controller.InvokeActionAsync(fieldContextBuilder.GraphMethod, resolutionContext);
162162
});
163163
}
164164
}

src/unit-tests/graphql-aspnet-subscriptions-tests/Engine/DefaultEventRouterTests.cs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace GraphQL.AspNet.Tests.Engine
1717
using GraphQL.AspNet.Schemas;
1818
using GraphQL.AspNet.SubscriptionServer;
1919
using GraphQL.AspNet.Tests.Framework.CommonHelpers;
20-
using Moq;
20+
using NSubstitute;
2121
using NUnit.Framework;
2222

2323
[TestFixture]
@@ -26,8 +26,8 @@ public class DefaultEventRouterTests
2626
[Test]
2727
public void NullEventResultsInException()
2828
{
29-
var dispatcher = new Mock<ISubscriptionEventDispatchQueue>();
30-
var router = new DefaultSubscriptionEventRouter(dispatcher.Object);
29+
var dispatcher = Substitute.For<ISubscriptionEventDispatchQueue>();
30+
var router = new DefaultSubscriptionEventRouter(dispatcher);
3131

3232
Assert.Throws<ArgumentNullException>(() =>
3333
{
@@ -40,8 +40,8 @@ public void NullEventResultsInException()
4040
[Test]
4141
public void RemovingANullReceiverDoesNothing()
4242
{
43-
var dispatcher = new Mock<ISubscriptionEventDispatchQueue>();
44-
var router = new DefaultSubscriptionEventRouter(dispatcher.Object);
43+
var dispatcher = Substitute.For<ISubscriptionEventDispatchQueue>();
44+
var router = new DefaultSubscriptionEventRouter(dispatcher);
4545

4646
router.RemoveClient(null);
4747

@@ -51,11 +51,11 @@ public void RemovingANullReceiverDoesNothing()
5151
[Test]
5252
public void SubscribedReceiver_ReceivesRaisedEvent()
5353
{
54-
var receiver = new Mock<ISubscriptionClientProxy>();
55-
receiver.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
56-
var dispatcher = new Mock<ISubscriptionEventDispatchQueue>();
54+
var receiver = Substitute.For<ISubscriptionClientProxy>();
55+
receiver.Id.Returns(SubscriptionClientId.NewClientId());
56+
var dispatcher = Substitute.For<ISubscriptionEventDispatchQueue>();
5757

58-
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher.Object);
58+
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher);
5959

6060
var evt = new SubscriptionEvent()
6161
{
@@ -65,23 +65,23 @@ public void SubscribedReceiver_ReceivesRaisedEvent()
6565
Data = new TwoPropertyObject(),
6666
};
6767

68-
router.AddClient(receiver.Object, evt.ToSubscriptionEventName());
68+
router.AddClient(receiver, evt.ToSubscriptionEventName());
6969
router.RaisePublishedEvent(evt);
7070

71-
dispatcher.Verify(x => x.EnqueueEvent(receiver.Object.Id, evt, false), Times.Once, "Event1 never received");
71+
dispatcher.Received(1).EnqueueEvent(receiver.Id, evt, false);
7272

7373
router.Dispose();
7474
}
7575

7676
[Test]
7777
public void SubscribedReceiveer_TwoEvents_BothEventsAreDispatchedToTheReceiver()
7878
{
79-
var receiver = new Mock<ISubscriptionClientProxy>();
80-
receiver.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
79+
var receiver = Substitute.For<ISubscriptionClientProxy>();
80+
receiver.Id.Returns(SubscriptionClientId.NewClientId());
8181

82-
var dispatcher = new Mock<ISubscriptionEventDispatchQueue>();
82+
var dispatcher = Substitute.For<ISubscriptionEventDispatchQueue>();
8383

84-
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher.Object);
84+
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher);
8585

8686
var evt = new SubscriptionEvent()
8787
{
@@ -100,26 +100,26 @@ public void SubscribedReceiveer_TwoEvents_BothEventsAreDispatchedToTheReceiver()
100100
};
101101

102102
// add two events but remove the one being raised
103-
router.AddClient(receiver.Object, evt.ToSubscriptionEventName());
104-
router.AddClient(receiver.Object, evt2.ToSubscriptionEventName());
103+
router.AddClient(receiver, evt.ToSubscriptionEventName());
104+
router.AddClient(receiver, evt2.ToSubscriptionEventName());
105105
router.RaisePublishedEvent(evt);
106106
router.RaisePublishedEvent(evt2);
107107

108-
dispatcher.Verify(x => x.EnqueueEvent(receiver.Object.Id, evt, false), Times.Once, "Event1 never received");
109-
dispatcher.Verify(x => x.EnqueueEvent(receiver.Object.Id, evt2, false), Times.Once, "Event2 never received");
108+
dispatcher.Received(1).EnqueueEvent(receiver.Id, evt, false);
109+
dispatcher.Received(1).EnqueueEvent(receiver.Id, evt2, false);
110110

111111
router.Dispose();
112112
}
113113

114114
[Test]
115115
public void UnsubscribedReceiver_DoesNotReceivesRaisedEvent()
116116
{
117-
var receiver = new Mock<ISubscriptionClientProxy>();
118-
receiver.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
117+
var receiver = Substitute.For<ISubscriptionClientProxy>();
118+
receiver.Id.Returns(SubscriptionClientId.NewClientId());
119119

120-
var dispatcher = new Mock<ISubscriptionEventDispatchQueue>();
120+
var dispatcher = Substitute.For<ISubscriptionEventDispatchQueue>();
121121

122-
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher.Object);
122+
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher);
123123

124124
var evt = new SubscriptionEvent()
125125
{
@@ -139,27 +139,27 @@ public void UnsubscribedReceiver_DoesNotReceivesRaisedEvent()
139139

140140
// add two events but remove the one being raised
141141
// to ensure its not processed
142-
router.AddClient(receiver.Object, evt.ToSubscriptionEventName());
143-
router.AddClient(receiver.Object, evt2.ToSubscriptionEventName());
144-
router.RemoveClient(receiver.Object, evt.ToSubscriptionEventName());
142+
router.AddClient(receiver, evt.ToSubscriptionEventName());
143+
router.AddClient(receiver, evt2.ToSubscriptionEventName());
144+
router.RemoveClient(receiver, evt.ToSubscriptionEventName());
145145
router.RaisePublishedEvent(evt);
146146
router.RaisePublishedEvent(evt2);
147147

148-
dispatcher.Verify(x => x.EnqueueEvent(receiver.Object.Id, evt, false), Times.Never, "Event1 was received");
149-
dispatcher.Verify(x => x.EnqueueEvent(receiver.Object.Id, evt2, false), Times.Once, "Event2 never received");
148+
dispatcher.Received(0).EnqueueEvent(receiver.Id, evt, false);
149+
dispatcher.Received(1).EnqueueEvent(receiver.Id, evt2, false);
150150

151151
router.Dispose();
152152
}
153153

154154
[Test]
155155
public void UnsubscribedAllReceiver_DoesNotReceivesRaisedEvent()
156156
{
157-
var receiver = new Mock<ISubscriptionClientProxy>();
158-
receiver.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
157+
var receiver = Substitute.For<ISubscriptionClientProxy>();
158+
receiver.Id.Returns(SubscriptionClientId.NewClientId());
159159

160-
var dispatcher = new Mock<ISubscriptionEventDispatchQueue>();
160+
var dispatcher = Substitute.For<ISubscriptionEventDispatchQueue>();
161161

162-
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher.Object);
162+
var router = new DefaultSubscriptionEventRouter(dispatchQueue: dispatcher);
163163

164164
var evt = new SubscriptionEvent()
165165
{
@@ -178,14 +178,14 @@ public void UnsubscribedAllReceiver_DoesNotReceivesRaisedEvent()
178178
};
179179

180180
// add two events and ensure both are removed when not directly named
181-
router.AddClient(receiver.Object, evt.ToSubscriptionEventName());
182-
router.AddClient(receiver.Object, evt2.ToSubscriptionEventName());
183-
router.RemoveClient(receiver.Object);
181+
router.AddClient(receiver, evt.ToSubscriptionEventName());
182+
router.AddClient(receiver, evt2.ToSubscriptionEventName());
183+
router.RemoveClient(receiver);
184184
router.RaisePublishedEvent(evt);
185185
router.RaisePublishedEvent(evt2);
186186

187-
dispatcher.Verify(x => x.EnqueueEvent(receiver.Object.Id, evt, false), Times.Never, "Event1 was received");
188-
dispatcher.Verify(x => x.EnqueueEvent(receiver.Object.Id, evt2, false), Times.Never, "Event2 was received");
187+
dispatcher.Received(0).EnqueueEvent(receiver.Id, evt, false);
188+
dispatcher.Received(0).EnqueueEvent(receiver.Id, evt2, false);
189189

190190
router.Dispose();
191191
}

src/unit-tests/graphql-aspnet-subscriptions-tests/Engine/DefaultGlobalSubscriptionClientProxyCollectionTests.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace GraphQL.AspNet.Tests.Engine
1414
using GraphQL.AspNet.Interfaces.Subscriptions;
1515
using GraphQL.AspNet.SubscriptionServer;
1616
using Microsoft.VisualStudio.TestPlatform.Utilities;
17-
using Moq;
17+
using NSubstitute;
1818
using NUnit.Framework;
1919

2020
[TestFixture]
@@ -25,10 +25,10 @@ public void AddClient_WhenMaxNotReached_ClientIsAdded()
2525
{
2626
var collection = new DefaultGlobalSubscriptionClientProxyCollection(5);
2727

28-
var client = new Mock<ISubscriptionClientProxy>();
29-
client.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
28+
var client = Substitute.For<ISubscriptionClientProxy>();
29+
client.Id.Returns(SubscriptionClientId.NewClientId());
3030

31-
var result = collection.TryAddClient(client.Object);
31+
var result = collection.TryAddClient(client);
3232

3333
Assert.IsTrue(result);
3434
Assert.AreEqual(1, collection.Count);
@@ -39,14 +39,14 @@ public void AddClient_WhenMaxReached_ClientIsRejected()
3939
{
4040
var collection = new DefaultGlobalSubscriptionClientProxyCollection(1);
4141

42-
var client = new Mock<ISubscriptionClientProxy>();
43-
client.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
42+
var client = Substitute.For<ISubscriptionClientProxy>();
43+
client.Id.Returns(SubscriptionClientId.NewClientId());
4444

45-
var client2 = new Mock<ISubscriptionClientProxy>();
46-
client2.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
45+
var client2 = Substitute.For<ISubscriptionClientProxy>();
46+
client2.Id.Returns(SubscriptionClientId.NewClientId());
4747

48-
collection.TryAddClient(client.Object);
49-
var result = collection.TryAddClient(client2.Object);
48+
collection.TryAddClient(client);
49+
var result = collection.TryAddClient(client2);
5050

5151
Assert.IsFalse(result);
5252
Assert.AreEqual(1, collection.Count);
@@ -57,16 +57,16 @@ public void TryRemoveClient_ForExistingClient_ClientIsRemoved()
5757
{
5858
var collection = new DefaultGlobalSubscriptionClientProxyCollection(1);
5959

60-
var client = new Mock<ISubscriptionClientProxy>();
61-
client.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
60+
var client = Substitute.For<ISubscriptionClientProxy>();
61+
client.Id.Returns(SubscriptionClientId.NewClientId());
6262

63-
collection.TryAddClient(client.Object);
63+
collection.TryAddClient(client);
6464
Assert.AreEqual(1, collection.Count);
6565

66-
var result = collection.TryRemoveClient(client.Object.Id, out var obj);
66+
var result = collection.TryRemoveClient(client.Id, out var obj);
6767

6868
Assert.IsTrue(result);
69-
Assert.AreEqual(client.Object, obj);
69+
Assert.AreEqual(client, obj);
7070
Assert.AreEqual(0, collection.Count);
7171
}
7272

@@ -101,15 +101,15 @@ public void TryGetClient_ForExistingClient_ClientIsReturned()
101101
{
102102
var collection = new DefaultGlobalSubscriptionClientProxyCollection(1);
103103

104-
var client = new Mock<ISubscriptionClientProxy>();
105-
client.Setup(x => x.Id).Returns(SubscriptionClientId.NewClientId());
104+
var client = Substitute.For<ISubscriptionClientProxy>();
105+
client.Id.Returns(SubscriptionClientId.NewClientId());
106106

107-
collection.TryAddClient(client.Object);
107+
collection.TryAddClient(client);
108108

109-
var result = collection.TryGetClient(client.Object.Id, out var obj);
109+
var result = collection.TryGetClient(client.Id, out var obj);
110110

111111
Assert.IsTrue(result);
112-
Assert.AreEqual(client.Object, obj);
112+
Assert.AreEqual(client, obj);
113113
Assert.AreEqual(1, collection.Count);
114114
}
115115

0 commit comments

Comments
 (0)