Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit ec71110

Browse files
committed
Fix tests that use repository models
Convert them from using NSubstitute to creating the model object.
1 parent e2b74f9 commit ec71110

17 files changed

+124
-102
lines changed

src/GitHub.App/ViewModels/GitHubPane/PullRequestCreationViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public PullRequestCreationViewModel(
8383
.Subscribe(x =>
8484
{
8585
if (!x.Any(t => t.Equals(TargetBranch)))
86+
{
8687
TargetBranch = GitHubRepository.IsFork ? GitHubRepository.Parent.DefaultBranch : GitHubRepository.DefaultBranch;
88+
}
8789
});
8890

8991
SetupValidators();

test/GitHub.App.UnitTests/Services/PullRequestServiceTests.cs

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,11 @@ static PullRequestService CreatePullRequestService(Repository repo)
584584
static LocalRepositoryModel CreateLocalRepositoryModel(Repository repo)
585585
{
586586
var repoDir = repo.Info.WorkingDirectory;
587-
var repositoryModel = Substitute.For<LocalRepositoryModel>();
588-
repositoryModel.LocalPath.Returns(repoDir);
587+
var repositoryModel = new LocalRepositoryModel
588+
{
589+
LocalPath = repoDir
590+
};
591+
589592
return repositoryModel;
590593
}
591594

@@ -598,7 +601,8 @@ public async Task ExtractsExistingFile_Async()
598601
{
599602
var gitClient = MockGitClient();
600603
var target = CreateTarget(gitClient);
601-
var repository = Substitute.For<LocalRepositoryModel>();
604+
var repository = new LocalRepositoryModel { };
605+
602606
var fileContent = "file content";
603607
var pr = CreatePullRequest();
604608

@@ -620,7 +624,7 @@ public async Task CreatesEmptyFileForNonExistentFileAsync()
620624
{
621625
var gitClient = MockGitClient();
622626
var target = CreateTarget(gitClient);
623-
var repository = Substitute.For<LocalRepositoryModel>();
627+
var repository = new LocalRepositoryModel { };
624628
var pr = CreatePullRequest();
625629

626630
gitClient.ExtractFile(Arg.Any<IRepository>(), "123", "filename").Returns(GetFileTaskAsync(null));
@@ -647,7 +651,7 @@ public async Task CanChangeEncodingAsync(string encodingName)
647651
var fileContent = "file content";
648652
var gitClient = MockGitClient();
649653
var target = CreateTarget(gitClient);
650-
var repository = Substitute.For<LocalRepositoryModel>();
654+
var repository = new LocalRepositoryModel { };
651655
var pr = CreatePullRequest();
652656

653657
var expectedPath = Path.Combine(repoDir, fileName);
@@ -730,7 +734,7 @@ public async Task ShouldCheckoutExistingBranchAsync()
730734
var gitClient = MockGitClient();
731735
var service = CreateTarget(gitClient, MockGitService());
732736

733-
var localRepo = Substitute.For<LocalRepositoryModel>();
737+
var localRepo = new LocalRepositoryModel { };
734738

735739
var pr = new PullRequestDetailModel
736740
{
@@ -753,9 +757,10 @@ public async Task ShouldCheckoutLocalBranchAsync()
753757
{
754758
var gitClient = MockGitClient();
755759
var service = CreateTarget(gitClient, MockGitService());
756-
757-
var localRepo = Substitute.For<LocalRepositoryModel>();
758-
localRepo.CloneUrl.Returns(new UriString("https://foo.bar/owner/repo"));
760+
var localRepo = new LocalRepositoryModel
761+
{
762+
CloneUrl = new UriString("https://foo.bar/owner/repo")
763+
};
759764

760765
var pr = new PullRequestDetailModel
761766
{
@@ -782,9 +787,10 @@ public async Task ShouldCheckoutLocalBranchOwnerCaseMismatchAsync()
782787
{
783788
var gitClient = MockGitClient();
784789
var service = CreateTarget(gitClient, MockGitService());
785-
786-
var localRepo = Substitute.For<LocalRepositoryModel>();
787-
localRepo.CloneUrl.Returns(new UriString("https://foo.bar/Owner/repo"));
790+
var localRepo = new LocalRepositoryModel
791+
{
792+
CloneUrl = new UriString("https://foo.bar/Owner/repo")
793+
};
788794

789795
var pr = new PullRequestDetailModel
790796
{
@@ -811,9 +817,10 @@ public async Task ShouldCheckoutBranchFromForkAsync()
811817
{
812818
var gitClient = MockGitClient();
813819
var service = CreateTarget(gitClient, MockGitService());
814-
815-
var localRepo = Substitute.For<LocalRepositoryModel>();
816-
localRepo.CloneUrl.Returns(new UriString("https://foo.bar/owner/repo"));
820+
var localRepo = new LocalRepositoryModel
821+
{
822+
CloneUrl = new UriString("https://foo.bar/owner/repo")
823+
};
817824

818825
var pr = new PullRequestDetailModel
819826
{
@@ -844,9 +851,10 @@ public async Task ShouldUseUniquelyNamedRemoteForForkAsync()
844851
var gitClient = MockGitClient();
845852
var gitService = MockGitService();
846853
var service = CreateTarget(gitClient, gitService);
847-
848-
var localRepo = Substitute.For<LocalRepositoryModel>();
849-
localRepo.CloneUrl.Returns(new UriString("https://foo.bar/owner/repo"));
854+
var localRepo = new LocalRepositoryModel
855+
{
856+
CloneUrl = new UriString("https://foo.bar/owner/repo")
857+
};
850858

851859
using (var repo = gitService.GetRepository(localRepo.CloneUrl))
852860
{
@@ -881,7 +889,7 @@ public async Task ShouldReturnCorrectDefaultLocalBranchNameAsync()
881889
{
882890
var service = CreateTarget(MockGitClient(), MockGitService());
883891

884-
var localRepo = Substitute.For<LocalRepositoryModel>();
892+
var localRepo = new LocalRepositoryModel { };
885893
var result = await service.GetDefaultLocalBranchName(localRepo, 123, "Pull requests can be \"named\" all sorts of thing's (sic)");
886894
Assert.That("pr/123-pull-requests-can-be-named-all-sorts-of-thing-s-sic", Is.EqualTo(result));
887895
}
@@ -897,7 +905,7 @@ public async Task ShouldReturnCorrectDefaultLocalBranchNameForPullRequestsWithNo
897905
Substitute.For<IOperatingSystem>(),
898906
Substitute.For<IUsageTracker>());
899907

900-
var localRepo = Substitute.For<LocalRepositoryModel>();
908+
var localRepo = new LocalRepositoryModel { };
901909
var result = await service.GetDefaultLocalBranchName(localRepo, 123, "コードをレビューする準備ができたこと");
902910
Assert.That("pr/123", Is.EqualTo(result));
903911
}
@@ -907,7 +915,7 @@ public async Task DefaultLocalBranchNameShouldNotClashWithExistingBranchNamesAsy
907915
{
908916
var service = CreateTarget(MockGitClient(), MockGitService());
909917

910-
var localRepo = Substitute.For<LocalRepositoryModel>();
918+
var localRepo = new LocalRepositoryModel { };
911919
var result = await service.GetDefaultLocalBranchName(localRepo, 123, "foo1");
912920
Assert.That("pr/123-foo1-3", Is.EqualTo(result));
913921
}
@@ -919,9 +927,10 @@ public class TheGetLocalBranchesMethod
919927
public async Task ShouldReturnPullRequestBranchForPullRequestFromSameRepositoryAsync()
920928
{
921929
var service = CreateTarget(MockGitClient(), MockGitService());
922-
923-
var localRepo = Substitute.For<LocalRepositoryModel>();
924-
localRepo.CloneUrl.Returns(new UriString("https://github.com/foo/bar"));
930+
var localRepo = new LocalRepositoryModel
931+
{
932+
CloneUrl = new UriString("https://github.com/foo/bar")
933+
};
925934

926935
var result = await service.GetLocalBranches(localRepo, CreatePullRequest(fromFork: false));
927936

@@ -932,9 +941,10 @@ public async Task ShouldReturnPullRequestBranchForPullRequestFromSameRepositoryA
932941
public async Task ShouldReturnPullRequestBranchForPullRequestFromSameRepositoryOwnerCaseMismatchAsync()
933942
{
934943
var service = CreateTarget(MockGitClient(), MockGitService());
935-
936-
var localRepo = Substitute.For<LocalRepositoryModel>();
937-
localRepo.CloneUrl.Returns(new UriString("https://github.com/Foo/bar"));
944+
var localRepo = new LocalRepositoryModel
945+
{
946+
CloneUrl = new UriString("https://github.com/Foo/bar")
947+
};
938948

939949
var result = await service.GetLocalBranches(localRepo, CreatePullRequest(fromFork: false));
940950

@@ -964,8 +974,10 @@ public async Task ShouldReturnMarkedBranchForPullRequestFromForkAsync()
964974

965975
var service = CreateTarget(MockGitClient(), MockGitService(repo));
966976

967-
var localRepo = Substitute.For<LocalRepositoryModel>();
968-
localRepo.CloneUrl.Returns(new UriString("https://github.com/foo/bar.git"));
977+
var localRepo = new LocalRepositoryModel
978+
{
979+
CloneUrl = new UriString("https://github.com/foo/bar.git")
980+
};
969981

970982
var result = await service.GetLocalBranches(localRepo, CreatePullRequest(true));
971983

@@ -1003,9 +1015,10 @@ public async Task ShouldRemoveUnusedRemoteAsync()
10031015
var gitClient = MockGitClient();
10041016
var gitService = MockGitService();
10051017
var service = CreateTarget(gitClient, gitService);
1006-
1007-
var localRepo = Substitute.For<LocalRepositoryModel>();
1008-
localRepo.CloneUrl.Returns(new UriString("https://github.com/foo/bar"));
1018+
var localRepo = new LocalRepositoryModel
1019+
{
1020+
CloneUrl = new UriString("https://github.com/foo/bar")
1021+
};
10091022

10101023
using (var repo = gitService.GetRepository(localRepo.CloneUrl))
10111024
{

test/GitHub.App.UnitTests/Services/TeamExplorerContextTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ static TeamExplorerContext CreateTeamExplorerContext(
204204

205205
static LocalRepositoryModel CreateRepositoryModel(string path)
206206
{
207-
var repo = Substitute.For<LocalRepositoryModel>();
208-
repo.LocalPath.Returns(path);
209-
return repo;
207+
return new LocalRepositoryModel
208+
{
209+
LocalPath = path
210+
};
210211
}
211212

212213
static IVSGitExt CreateGitExt()

test/GitHub.App.UnitTests/ViewModels/Dialog/Clone/RepositoryCloneViewModelTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,8 @@ static RepositoryModel CreateRepositoryModel(string repo = "owner/repo")
460460

461461
static RepositoryModel CreateRepositoryModel(string owner, string name)
462462
{
463-
var repository = Substitute.For<RepositoryModel>();
464-
repository.Owner.Returns(owner);
465-
repository.Name.Returns(name);
466-
repository.CloneUrl.Returns(CreateGitHubUrl(owner, name));
467-
return repository;
463+
var cloneUrl = CreateGitHubUrl(owner, name);
464+
return new RepositoryModel(name, cloneUrl);
468465
}
469466

470467
static UriString CreateGitHubUrl(string owner, string repo)

test/GitHub.App.UnitTests/ViewModels/GitHubPane/GitHubPaneViewModelTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,10 @@ static INavigationViewModel CreateNavigator()
282282

283283
static ITeamExplorerContext CreateTeamExplorerContext(string repositoryCloneUrl)
284284
{
285-
var repository = Substitute.For<LocalRepositoryModel>();
286-
repository.CloneUrl.Returns(new UriString(repositoryCloneUrl));
285+
var repository = new LocalRepositoryModel
286+
{
287+
CloneUrl = new UriString(repositoryCloneUrl)
288+
};
287289
var result = Substitute.For<ITeamExplorerContext>();
288290
result.ActiveRepository.Returns(repository);
289291
return result;

test/GitHub.App.UnitTests/ViewModels/GitHubPane/IssueListViewModelBaseTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ protected static LocalRepositoryModel CreateLocalRepository(
9696
string owner = "owner",
9797
string name = "name")
9898
{
99-
var result = Substitute.For<LocalRepositoryModel>();
100-
result.CloneUrl.Returns(new UriString($"https://giuthub.com/{owner}/{name}"));
101-
result.Owner.Returns(owner);
102-
result.Name.Returns(name);
103-
return result;
99+
return new LocalRepositoryModel
100+
{
101+
CloneUrl = new UriString($"https://giuthub.com/{owner}/{name}"),
102+
Name = name
103+
};
104104
}
105105

106106
protected static IPullRequestSessionManager CreateSessionManager(PullRequestDetailModel pullRequest = null)

test/GitHub.App.UnitTests/ViewModels/GitHubPane/PullRequestDetailViewModelTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,16 @@ static Tuple<PullRequestDetailViewModel, IPullRequestService> CreateTargetAndSer
550550
int behindBy = 0,
551551
IPullRequestSessionManager sessionManager = null)
552552
{
553-
var repository = Substitute.For<LocalRepositoryModel>();
553+
var repository = new LocalRepositoryModel
554+
{
555+
CloneUrl = new UriString(Uri.ToString()),
556+
LocalPath = @"C:\projects\ThisRepo",
557+
Name = "repo"
558+
};
559+
554560
var currentBranchModel = new BranchModel(currentBranch, repository);
555561
var gitService = Substitute.For<IGitService>();
556562
gitService.GetBranch(repository).Returns(currentBranchModel);
557-
repository.CloneUrl.Returns(new UriString(Uri.ToString()));
558-
repository.LocalPath.Returns(@"C:\projects\ThisRepo");
559-
repository.Name.Returns("repo");
560563

561564
var pullRequestService = Substitute.For<IPullRequestService>();
562565

test/GitHub.App.UnitTests/ViewModels/GitHubPane/PullRequestFilesViewModelTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ static IPullRequestSession CreateSession()
100100
{
101101
var author = Substitute.For<IAccount>();
102102

103-
var repository = Substitute.For<LocalRepositoryModel>();
104-
repository.LocalPath.Returns(@"C:\Foo");
103+
var repository = new LocalRepositoryModel { LocalPath = @"C:\Foo" };
105104

106105
var result = Substitute.For<IPullRequestSession>();
107106
result.LocalRepository.Returns(repository);

test/GitHub.App.UnitTests/ViewModels/GitHubPane/PullRequestReviewAuthoringViewModelTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,11 @@ static IPullRequestSessionManager CreateSessionManager(
607607

608608
static LocalRepositoryModel CreateLocalRepositoryModel()
609609
{
610-
var result = Substitute.For<LocalRepositoryModel>();
611-
result.CloneUrl.Returns(new UriString("https://github.com/owner/repo"));
612-
result.Owner.Returns("owner");
613-
result.Name.Returns("repo");
610+
var result = new LocalRepositoryModel
611+
{
612+
CloneUrl = new UriString("https://github.com/owner/repo"),
613+
Name = "repo"
614+
};
614615
return result;
615616
}
616617

test/GitHub.App.UnitTests/ViewModels/GitHubPane/PullRequestUserReviewsViewModelTests.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using GitHub.Factories;
66
using GitHub.Models;
7+
using GitHub.Primitives;
78
using GitHub.Services;
89
using GitHub.ViewModels.GitHubPane;
910
using NSubstitute;
@@ -207,19 +208,13 @@ PullRequestUserReviewsViewModel CreateTarget(
207208
sessionManager);
208209
}
209210

210-
IModelServiceFactory CreateFactory(IModelService modelService)
211-
{
212-
var result = Substitute.For<IModelServiceFactory>();
213-
result.CreateAsync(null).ReturnsForAnyArgs(modelService);
214-
return result;
215-
}
216-
217211
LocalRepositoryModel CreateRepository(string owner = "owner", string name = "repo")
218212
{
219-
var result = Substitute.For<LocalRepositoryModel>();
220-
result.Owner.Returns(owner);
221-
result.Name.Returns(name);
222-
return result;
213+
return new LocalRepositoryModel
214+
{
215+
CloneUrl = new UriString($"https://github.com/{owner}/{name}"),
216+
Name = name
217+
};
223218
}
224219
}
225220
}

0 commit comments

Comments
 (0)