Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
global using ApprovalTests.Reporters;
global using Castle.DynamicProxy.Generators.Emitters;
global using FluentAssertions;
global using Moq;
global using NSubstitute;
global using ReqnrollConnector;
global using ReqnrollConnector.Discovery;
global using ReqnrollConnector.Logging;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="ApprovalTests" Version="5.9.0" /> <!-- cannot upgrade to 6.0 because of test execution issues -->
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NSubstitute " Version="5.3.0" />
<PackageReference Include="NuGet.CommandLine" Version="6.11.0" CopyTools="true" CopyToolsTarget="Tools-NuGet">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion Tests/Reqnroll.VisualStudio.Specs/ImplicitUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
global using Microsoft.VisualStudio.Text.Editor;
global using Microsoft.VisualStudio.Text.Tagging;
global using Microsoft.VisualStudio.Threading;
global using Moq;
global using NSubstitute;
global using Reqnroll.SampleProjectGenerator;
global using Reqnroll.VisualStudio.Common;
global using Reqnroll.VisualStudio.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.29" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NSubstitute " Version="5.3.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public DeveroomSteps(ITestOutputHelper outputHelper, StubIdeScope stubIdeScope)
{
_outputHelper = outputHelper;
_stubIdeScope = stubIdeScope;

_stubIdeScope.Setup(s =>
s.FireAndForgetOnBackgroundThread(It.IsAny<Func<CancellationToken, Task>>(), It.IsAny<string>()))
.Callback(FireAndForgetCallBack);

_stubIdeScope.SetupFireAndForgetOnBackgroundThread(FireAndForgetCallBack);

async void FireAndForgetCallBack(Func<CancellationToken, Task> action, string _)
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#nullable disable
using Microsoft.Build.Framework.XamlTypes;
using ScenarioBlock = Reqnroll.VisualStudio.Editor.Services.Parser.ScenarioBlock;

namespace Reqnroll.VisualStudio.Specs.StepDefinitions;

[Binding]
Expand All @@ -21,10 +18,7 @@ public class ProjectSystemSteps : Steps
public ProjectSystemSteps(StubIdeScope stubIdeScope)
{
_ideScope = stubIdeScope;
_ideScope.Setup(s =>
s.FireAndForgetOnBackgroundThread(It.IsAny<Func<CancellationToken, Task>>(), It.IsAny<string>()))
.Callback((Func<CancellationToken, Task> action, string _) =>
action(_ideScope.BackgroundTaskTokenSource.Token));
_ideScope.SetupFireAndForgetOnBackgroundThread((action, callerName) => action(_ideScope.BackgroundTaskTokenSource.Token));
}

private StubIdeActions ActionsMock => (StubIdeActions) _ideScope.Actions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace Reqnroll.VisualStudio.Tests.Analytics;

public class AnalyticsTransmitterTests
{
private Mock<IAnalyticsTransmitterSink> analyticsTransmitterSinkStub;
private Mock<IEnableAnalyticsChecker> enableAnalyticsCheckerStub;
private IAnalyticsTransmitterSink analyticsTransmitterSinkStub;
private IEnableAnalyticsChecker enableAnalyticsCheckerStub;

[Fact]
public void Should_NotSendAnalytics_WhenDisabled()
{
var sut = CreateSut();
GivenAnalyticsDisabled();

sut.TransmitEvent(It.IsAny<IAnalyticsEvent>());
sut.TransmitEvent(Substitute.For<IAnalyticsEvent>());

enableAnalyticsCheckerStub.Verify(analyticsChecker => analyticsChecker.IsEnabled(), Times.Once);
analyticsTransmitterSinkStub.Verify(sink => sink.TransmitEvent(It.IsAny<IAnalyticsEvent>()), Times.Never);
enableAnalyticsCheckerStub.Received(1).IsEnabled();
analyticsTransmitterSinkStub.DidNotReceive().TransmitEvent(Arg.Any<IAnalyticsEvent>());
}

[Fact]
Expand All @@ -27,10 +27,10 @@ public void Should_SendAnalytics_WhenEnabled()
var sut = CreateSut();
GivenAnalyticsEnabled();

sut.TransmitEvent(It.IsAny<IAnalyticsEvent>());
sut.TransmitEvent(Substitute.For<IAnalyticsEvent>());

enableAnalyticsCheckerStub.Verify(analyticsChecker => analyticsChecker.IsEnabled(), Times.Once);
analyticsTransmitterSinkStub.Verify(sink => sink.TransmitEvent(It.IsAny<IAnalyticsEvent>()), Times.Once);
enableAnalyticsCheckerStub.Received(1).IsEnabled();
analyticsTransmitterSinkStub.Received(1).TransmitEvent(Arg.Any<IAnalyticsEvent>());
}

[Theory]
Expand All @@ -44,24 +44,23 @@ public void Should_TransmitEvents(string eventName)

sut.TransmitEvent(new GenericEvent(eventName));

analyticsTransmitterSinkStub.Verify(
sink => sink.TransmitEvent(It.Is<IAnalyticsEvent>(ae => ae.EventName == eventName)), Times.Once);
analyticsTransmitterSinkStub.Received(1).TransmitEvent(Arg.Is<IAnalyticsEvent>(ae => ae.EventName == eventName));
}

private void GivenAnalyticsEnabled()
{
enableAnalyticsCheckerStub.Setup(analyticsChecker => analyticsChecker.IsEnabled()).Returns(true);
enableAnalyticsCheckerStub.IsEnabled().Returns(true);
}

private void GivenAnalyticsDisabled()
{
enableAnalyticsCheckerStub.Setup(analyticsChecker => analyticsChecker.IsEnabled()).Returns(false);
enableAnalyticsCheckerStub.IsEnabled().Returns(false);
}

public AnalyticsTransmitter CreateSut()
{
analyticsTransmitterSinkStub = new Mock<IAnalyticsTransmitterSink>();
enableAnalyticsCheckerStub = new Mock<IEnableAnalyticsChecker>();
return new AnalyticsTransmitter(analyticsTransmitterSinkStub.Object, enableAnalyticsCheckerStub.Object);
analyticsTransmitterSinkStub = Substitute.For<IAnalyticsTransmitterSink>();
enableAnalyticsCheckerStub = Substitute.For<IEnableAnalyticsChecker>();
return new AnalyticsTransmitter(analyticsTransmitterSinkStub, enableAnalyticsCheckerStub);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Reqnroll.VisualStudio.Tests.Analytics;
public class FileUserIdStoreTests
{
private const string UserId = "491ed5c0-9f25-4c27-941a-19b17cc81c87";
private Mock<IFileSystemForVs> fileSystemStub;
private IFileSystemForVs fileSystemStub;

[Fact]
public void Should_GetUserIdFromFile_WhenFileExists()
Expand All @@ -29,30 +29,29 @@ public void Should_PersistNewlyGeneratedUserId_WhenNoUserIdExists()
string userId = sut.GetUserId();

userId.Should().NotBeEmpty();
fileSystemStub.Verify(fileSystem => fileSystem.File.WriteAllText(FileUserIdStore.UserIdFilePath, userId),
Times.Once());
fileSystemStub.File.Received(1).WriteAllText(FileUserIdStore.UserIdFilePath, userId);
}


public FileUserIdStore CreateSut()
{
fileSystemStub = new Mock<IFileSystemForVs>();
return new FileUserIdStore(fileSystemStub.Object);
fileSystemStub = Substitute.For<IFileSystemForVs>();
return new FileUserIdStore(fileSystemStub);
}

private void GivenFileExists()
{
fileSystemStub.Setup(fileSystem => fileSystem.File.Exists(It.IsAny<string>())).Returns(true);
fileSystemStub.File.Exists(Arg.Any<string>()).Returns(true);
}

private void GivenFileDoesNotExists()
{
fileSystemStub.Setup(fileSystem => fileSystem.File.Exists(It.IsAny<string>())).Returns(false);
fileSystemStub.Setup(fileSystem => fileSystem.Directory.Exists(It.IsAny<string>())).Returns(true);
fileSystemStub.File.Exists(Arg.Any<string>()).Returns(false);
fileSystemStub.Directory.Exists(Arg.Any<string>()).Returns(true);
}

private void GivenUserIdStringInFile(string userIdString)
{
fileSystemStub.Setup(fileSystem => fileSystem.File.ReadAllText(It.IsAny<string>())).Returns(userIdString);
fileSystemStub.File.ReadAllText(Arg.Any<string>()).Returns(userIdString);
}
}
20 changes: 6 additions & 14 deletions Tests/Reqnroll.VisualStudio.Tests/Discovery/DiscoveryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ public DiscoveryTests(ITestOutputHelper testOutputHelper)

private Sut ArrangeSut()
{
var bindingRegistryCache = new StubProjectBindingRegistryCache();
var bindingRegistryCache = Substitute.ForPartsOf<StubProjectBindingRegistryCache>();
var projectScope = new InMemoryStubProjectScope(_testOutputHelper);
var discoveryResultProvider = new StubDiscoveryResultProvider();
#pragma warning disable VSTHRD002
projectScope.StubIdeScope
.Setup(
s => s.FireAndForgetOnBackgroundThread(It.IsAny<Func<CancellationToken, Task>>(), It.IsAny<string>()))
.Callback((Func<CancellationToken, Task> action, string _)
=> action(projectScope.StubIdeScope.BackgroundTaskTokenSource.Token).Wait());
projectScope.StubIdeScope.SetupFireAndForgetOnBackgroundThread((action, callerName) => action(projectScope.StubIdeScope.BackgroundTaskTokenSource.Token).Wait());
#pragma warning restore VSTHRD002

InMemoryStubProjectBuilder.CreateOutputAssembly(projectScope);

return new Sut(bindingRegistryCache, projectScope, discoveryResultProvider);
return Substitute.For<Sut>(bindingRegistryCache, projectScope, discoveryResultProvider);
}

[Fact]
Expand All @@ -48,8 +44,7 @@ public void TriggerDiscoveryUpdatesTheCache()
discoveryService.TriggerDiscovery();

//assert
sut.BindingRegistryCache.Verify(c =>
c.Update(It.IsAny<Func<ProjectBindingRegistry, Task<ProjectBindingRegistry>>>()));
sut.BindingRegistryCache.Received().Update(Arg.Any<Func<ProjectBindingRegistry, Task<ProjectBindingRegistry>>>());
sut.BindingRegistryCache.Value.Version.Should().NotBe(1);
}

Expand All @@ -65,8 +60,7 @@ public void TriggersCacheUpdateOnEvents(LabeledTestData<Action<Sut>> triggerEven
triggerEvent.Data(sut);

//assert
sut.BindingRegistryCache.Verify(c =>
c.Update(It.IsAny<Func<ProjectBindingRegistry, Task<ProjectBindingRegistry>>>()));
sut.BindingRegistryCache.Received().Update(Arg.Any<Func<ProjectBindingRegistry, Task<ProjectBindingRegistry>>>());
sut.BindingRegistryCache.Value.Version.Should().NotBe(1);
}

Expand All @@ -84,9 +78,7 @@ public void DoNotTriggersCacheUpdateOnEventsForTheSameProject(LabeledTestData<Ac
triggerEvent.Data(sut);

//assert
sut.BindingRegistryCache.Verify(c =>
c.Update(It.IsAny<Func<ProjectBindingRegistry, Task<ProjectBindingRegistry>>>()), Times.Once,
"the cache update have to be called only once when the project haven't changed");
sut.BindingRegistryCache.Received(1).Update(Arg.Any<Func<ProjectBindingRegistry, Task<ProjectBindingRegistry>>>()); // the cache update have to be called only once when the project haven't changed
sut.BindingRegistryCache.Value.Should().BeSameAs(bindingRegistry, "the cache must not be modified");
sut.ProjectScope.StubIdeScope.StubLogger.Messages.Where(m => m == "Projects built or settings initialized")
.Should().HaveCount(2, "the event is fired twice");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ public void ParallelUpdate()
{
//arrange
var start = DateTimeOffset.UtcNow;
var ideScope = new Mock<IIdeScope>(MockBehavior.Strict);
var ideScope = Substitute.For<IIdeScope>();
var stubLogger = new StubLogger();
var logger = new DeveroomCompositeLogger();
logger.Add(new DeveroomXUnitLogger(_testOutputHelper));
logger.Add(stubLogger);

ideScope.SetupGet(s => s.Logger).Returns(logger);
ideScope.Setup(s => s.CalculateSourceLocationTrackingPositions(It.IsAny<IEnumerable<SourceLocation>>()));
ideScope.Logger.Returns(logger);
ideScope.CalculateSourceLocationTrackingPositions(Arg.Any<IEnumerable<SourceLocation>>());

var projectBindingRegistryCache = new ProjectBindingRegistryCache(ideScope.Object);
var projectBindingRegistryCache = new ProjectBindingRegistryCache(ideScope);

var oldVersions = new ConcurrentQueue<int>();
var initialRegistry = new ProjectBindingRegistry(Array.Empty<ProjectStepDefinitionBinding>(), Array.Empty<ProjectHookBinding>(), 123456);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class CommentUncommentCommandTests

public CommentUncommentCommandTests(ITestOutputHelper testOutputHelper)
{
_taggerProvider = new Mock<IDeveroomTaggerProvider>().Object;
_taggerProvider = Substitute.For<IDeveroomTaggerProvider>();
_ideScope = new StubIdeScope(testOutputHelper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class DeveroomGherkinParserTests
public void Should_provide_parse_result_when_unexpected_end_of_file()
{
var sut = new DeveroomGherkinParser(new ReqnrollGherkinDialectProvider("en-US"),
new Mock<IMonitoringService>().Object);
Substitute.For<IMonitoringService>());

var result = sut.ParseAndCollectErrors(@"
Feature: Addition
Expand All @@ -24,7 +24,7 @@ public void Should_provide_parse_result_when_unexpected_end_of_file()
public void Should_tolerate_backslash_at_end_of_line_in_DataTable()
{
var sut = new DeveroomGherkinParser(new ReqnrollGherkinDialectProvider("en-US"),
new Mock<IMonitoringService>().Object);
Substitute.For<IMonitoringService>());

var result = sut.ParseAndCollectErrors(@"
Feature: Addition
Expand All @@ -42,7 +42,7 @@ When I press
public void Should_tolerate_unfinished_DataTable()
{
var sut = new DeveroomGherkinParser(new ReqnrollGherkinDialectProvider("en-US"),
new Mock<IMonitoringService>().Object);
Substitute.For<IMonitoringService>());

var result = sut.ParseAndCollectErrors(@"
Feature: Addition
Expand All @@ -60,7 +60,7 @@ When I press
public void Should_provide_parse_result_when_file_ends_with_open_docstring()
{
var sut = new DeveroomGherkinParser(new ReqnrollGherkinDialectProvider("en-US"),
new Mock<IMonitoringService>().Object);
Substitute.For<IMonitoringService>());

var result = sut.ParseAndCollectErrors(@"
Feature: Addition
Expand Down
Loading
Loading