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

Commit 4c67081

Browse files
committed
Add tests for PR creation service and view model
1 parent 9c6eb80 commit 4c67081

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Reactive.Linq;
2+
using System.Threading.Tasks;
3+
using NSubstitute;
4+
using Xunit;
5+
using UnitTests;
6+
using GitHub.Models;
7+
using System;
8+
using GitHub.Services;
9+
10+
public class PullRequestServiceTests : TestBaseClass
11+
{
12+
[Fact]
13+
public async Task CreatePullRequestAllArgsMandatory()
14+
{
15+
var serviceProvider = Substitutes.ServiceProvider;
16+
var service = new PullRequestService();
17+
18+
IRepositoryHost host = null;
19+
ISimpleRepositoryModel repository = null;
20+
string title = null;
21+
string body = null;
22+
IBranch source = null;
23+
IBranch target = null;
24+
25+
Assert.Throws<ArgumentNullException>(() => service.CreatePullRequest(host, repository, title, body, source, target));
26+
27+
host = serviceProvider.GetRepositoryHosts().GitHubHost;
28+
Assert.Throws<ArgumentNullException>(() => service.CreatePullRequest(host, repository, title, body, source, target));
29+
30+
repository = new SimpleRepositoryModel("name", new GitHub.Primitives.UriString("http://github.com/github/stuff"));
31+
Assert.Throws<ArgumentNullException>(() => service.CreatePullRequest(host, repository, title, body, source, target));
32+
33+
title = "a title";
34+
Assert.Throws<ArgumentNullException>(() => service.CreatePullRequest(host, repository, title, body, source, target));
35+
36+
body = "a body";
37+
Assert.Throws<ArgumentNullException>(() => service.CreatePullRequest(host, repository, title, body, source, target));
38+
39+
source = new BranchModel() { Name = "source" };
40+
Assert.Throws<ArgumentNullException>(() => service.CreatePullRequest(host, repository, title, body, source, target));
41+
42+
target = new BranchModel() { Name = "target" };
43+
var pr = await service.CreatePullRequest(host, repository, title, body, source, target);
44+
45+
Assert.NotNull(pr);
46+
}
47+
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reactive.Linq;
2+
using System.Threading.Tasks;
3+
using NSubstitute;
4+
using Xunit;
5+
using UnitTests;
6+
using GitHub.Models;
7+
using System;
8+
using GitHub.Services;
9+
using GitHub.ViewModels;
10+
11+
public class PullRequestCreationViewModelTests : TempFileBaseClass
12+
{
13+
[Fact]
14+
public async Task NullDescriptionBecomesEmptyBody()
15+
{
16+
var serviceProvider = Substitutes.ServiceProvider;
17+
var service = new PullRequestService();
18+
var notifications = Substitute.For<INotificationService>();
19+
20+
var host = serviceProvider.GetRepositoryHosts().GitHubHost;
21+
var ms = Substitute.For<IModelService>();
22+
host.ModelService.Returns(ms);
23+
24+
var repository = new SimpleRepositoryModel("name", new GitHub.Primitives.UriString("http://github.com/github/stuff"));
25+
var title = "a title";
26+
27+
var vm = new PullRequestCreationViewModel(host, repository, service, notifications);
28+
vm.SourceBranch = new BranchModel() { Name = "source" };
29+
vm.TargetBranch = new BranchModel() { Name = "target" };
30+
vm.PRTitle = title;
31+
32+
await vm.CreatePullRequest.ExecuteAsync();
33+
ms.Received().CreatePullRequest(repository, vm.PRTitle, String.Empty, vm.SourceBranch, vm.TargetBranch);
34+
}
35+
36+
}

src/UnitTests/Substitutes.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using GitHub.Authentication;
33
using GitHub.Models;
44
using GitHub.Services;
5+
using GitHub.VisualStudio;
56
using Microsoft.VisualStudio.ComponentModelHost;
67
using NSubstitute;
78
using Rothko;
@@ -65,6 +66,7 @@ public static IOperatingSystem OperatingSystem
6566
public static IConnectionManager ConnectionManager { get { return Substitute.For<IConnectionManager>(); } }
6667
public static ITwoFactorChallengeHandler TwoFactorChallengeHandler { get { return Substitute.For<ITwoFactorChallengeHandler>(); } }
6768
public static IGistPublishService GistPublishService { get { return Substitute.For<IGistPublishService>(); } }
69+
public static IPullRequestService PullRequestService { get { return Substitute.For<IPullRequestService>(); } }
6870

6971
/// <summary>
7072
/// This returns a service provider with everything mocked except for
@@ -127,6 +129,7 @@ public static IServiceProvider GetServiceProvider(
127129
ret.GetService(typeof(IAvatarProvider)).Returns(avatarProvider);
128130
ret.GetService(typeof(ITwoFactorChallengeHandler)).Returns(TwoFactorChallengeHandler);
129131
ret.GetService(typeof(IGistPublishService)).Returns(GistPublishService);
132+
ret.GetService(typeof(IPullRequestService)).Returns(PullRequestService);
130133
return ret;
131134
}
132135

@@ -198,5 +201,10 @@ public static IGistPublishService GetGistPublishService(this IServiceProvider pr
198201
{
199202
return provider.GetService(typeof(IGistPublishService)) as IGistPublishService;
200203
}
204+
205+
public static IPullRequestService GetPullRequestsService(this IServiceProvider provider)
206+
{
207+
return provider.GetService(typeof(IPullRequestService)) as IPullRequestService;
208+
}
201209
}
202210
}

src/UnitTests/UnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,13 @@
166166
<Compile Include="GitHub.App\Models\RepositoryModelTests.cs" />
167167
<Compile Include="GitHub.App\Services\AvatarProviderTests.cs" />
168168
<Compile Include="GitHub.App\Services\GitClientTests.cs" />
169+
<Compile Include="GitHub.App\Services\PullRequestServiceTests.cs" />
169170
<Compile Include="GitHub.App\Services\RepositoryCloneServiceTests.cs" />
170171
<Compile Include="GitHub.App\Services\RepositoryCreationServiceTests.cs" />
171172
<Compile Include="GitHub.App\ViewModels\GistCreationViewModelTests.cs" />
172173
<Compile Include="GitHub.App\ViewModels\LoginControlViewModelTests.cs" />
173174
<Compile Include="GitHub.App\ViewModels\LoginToGitHubViewModelTests.cs" />
175+
<Compile Include="GitHub.App\ViewModels\PullRequestCreationViewModelTests.cs" />
174176
<Compile Include="GitHub.App\ViewModels\PullRequestListViewModelTests.cs" />
175177
<Compile Include="GitHub.App\ViewModels\RepositoryCloneViewModelTests.cs" />
176178
<Compile Include="GitHub.App\ViewModels\RepositoryCreationViewModelTests.cs" />

0 commit comments

Comments
 (0)