From f1edf200f02d82248e9bda3ded8cd0ef3f1cdc23 Mon Sep 17 00:00:00 2001 From: Gabriela Trutan Date: Mon, 7 Oct 2024 16:20:25 +0200 Subject: [PATCH] SLVS-1490 Refactor test class (#5729) --- .../Binding/BindingSuggestionHandlerTests.cs | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Integration.UnitTests/Binding/BindingSuggestionHandlerTests.cs b/src/Integration.UnitTests/Binding/BindingSuggestionHandlerTests.cs index 9e50da80c..0fe1167a3 100644 --- a/src/Integration.UnitTests/Binding/BindingSuggestionHandlerTests.cs +++ b/src/Integration.UnitTests/Binding/BindingSuggestionHandlerTests.cs @@ -31,6 +31,24 @@ namespace SonarLint.VisualStudio.Integration.UnitTests.Binding; [TestClass] public class BindingSuggestionHandlerTests { + private BindingSuggestionHandler testSubject; + private INotificationService notificationService; + private IActiveSolutionBoundTracker activeSolutionBoundTracker; + private IIDEWindowService ideWindowService; + private IConnectedModeUIManager connectedModeManager; + private IBrowserService browserService; + + [TestInitialize] + public void TestInitialize() + { + notificationService = Substitute.For(); + activeSolutionBoundTracker = Substitute.For(); + ideWindowService = Substitute.For(); + connectedModeManager = Substitute.For(); + browserService = Substitute.For(); + testSubject = new BindingSuggestionHandler(notificationService, activeSolutionBoundTracker, ideWindowService, connectedModeManager, browserService); + } + [TestMethod] public void MefCtor_CheckExports() { @@ -47,9 +65,8 @@ public void MefCtor_CheckExports() [DataRow(SonarLintMode.Connected)] public void Notify_BringsWindowToFront(SonarLintMode sonarLintMode) { - var ideWindowService = Substitute.For(); + MockCurrentConfiguration(sonarLintMode); - var testSubject = CreateTestSubject(sonarLintMode: sonarLintMode, ideWindowService: ideWindowService); testSubject.Notify(); ideWindowService.Received().BringToFront(); @@ -58,9 +75,8 @@ public void Notify_BringsWindowToFront(SonarLintMode sonarLintMode) [TestMethod] public void Notify_WithStandaloneProject_PromptsToConnect() { - var notificationService = Substitute.For(); + MockCurrentConfiguration(SonarLintMode.Standalone); - var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Standalone, notificationService: notificationService); testSubject.Notify(); notificationService.Received().ShowNotification(Arg.Is( @@ -73,9 +89,8 @@ public void Notify_WithStandaloneProject_PromptsToConnect() [TestMethod] public void Notify_WithBoundProject_ShowsConflictMessage() { - var notificationService = Substitute.For(); + MockCurrentConfiguration(SonarLintMode.Connected); - var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Connected, notificationService: notificationService); testSubject.Notify(); notificationService.Received().ShowNotification(Arg.Is( @@ -86,13 +101,12 @@ public void Notify_WithBoundProject_ShowsConflictMessage() } [TestMethod] - public void Notify_ConnectAction_OpensSonarQubePage() + public void Notify_ConnectAction_ShowsManageBindingDialog() { - var notificationService = Substitute.For(); - var connectedModeManager = Substitute.For(); + MockCurrentConfiguration(SonarLintMode.Standalone); - var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Standalone, notificationService: notificationService, connectedModeUiManager: connectedModeManager); testSubject.Notify(); + var notification = (Notification)notificationService.ReceivedCalls().Single().GetArguments().Single(); var connectAction = notification.Actions.First(x => x.CommandText.Equals(BindingStrings.BindingSuggestionConnect)); @@ -105,11 +119,10 @@ public void Notify_ConnectAction_OpensSonarQubePage() [TestMethod] public void Notify_LearnMoreAction_OpensDocumentationInBrowser() { - var notificationService = Substitute.For(); - var browserService = Substitute.For(); + MockCurrentConfiguration(SonarLintMode.Standalone); - var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Standalone, notificationService: notificationService, browserService: browserService); testSubject.Notify(); + var notification = (Notification)notificationService.ReceivedCalls().Single().GetArguments().Single(); var connectAction = notification.Actions.First(x => x.CommandText.Equals(BindingStrings.BindingSuggestionLearnMore)); @@ -119,20 +132,10 @@ public void Notify_LearnMoreAction_OpensDocumentationInBrowser() browserService.Received().Navigate(DocumentationLinks.OpenInIdeBindingSetup); } - private BindingSuggestionHandler CreateTestSubject(SonarLintMode sonarLintMode, - INotificationService notificationService = null, - IIDEWindowService ideWindowService = null, - IConnectedModeUIManager connectedModeUiManager = null, - IBrowserService browserService = null) + private void MockCurrentConfiguration(SonarLintMode sonarLintMode) { - notificationService ??= Substitute.For(); - var activeSolutionBoundTracker = Substitute.For(); - ideWindowService ??= Substitute.For(); - connectedModeUiManager ??= Substitute.For(); - browserService ??= Substitute.For(); - - activeSolutionBoundTracker.CurrentConfiguration.Returns(new BindingConfiguration(new BoundServerProject("solution", "server project", new ServerConnection.SonarCloud("org")), sonarLintMode, "a-directory")); - - return new BindingSuggestionHandler(notificationService, activeSolutionBoundTracker, ideWindowService, connectedModeUiManager, browserService); + activeSolutionBoundTracker.CurrentConfiguration.Returns(new BindingConfiguration( + new BoundServerProject("solution", "server project", new ServerConnection.SonarCloud("org")), sonarLintMode, + "a-directory")); } }