Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c5db70a

Browse files
author
Hariharan Subramanian
committed
Added minimal unit Tests using Nsubstitute for Answers Client
1 parent d046bc1 commit c5db70a

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

‎Sample/StackExchange.NET_Example/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using StackExchange.NET.Models;
66

77
namespace StackExchange.NET_Example
8-
{
8+
{
99
class Program
1010
{
1111
static void Main(string[] args)
@@ -17,6 +17,7 @@ static void Main(string[] args)
1717
Page = 1,
1818
Sort = Sort.Votes
1919
};
20+
var res = client.Answers.GetAllAnswers(null);
2021
// var answers = client.Answers.GetAllAnswers(queryString);
2122
var ids = new List<string>()
2223
{

‎StackExchange.NET/StackExchange.NET.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29215.179
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StackExchange.NET_Example", "..\Sample\StackExchange.NET_Example\StackExchange.NET_Example.csproj", "{5BBD81D3-5728-4C8D-8DF1-55C365AD7EFD}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "..\Sample\StackExchange.NET_Example\Example.csproj", "{5BBD81D3-5728-4C8D-8DF1-55C365AD7EFD}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackExchange.NET", "StackExchange.NET\StackExchange.NET.csproj", "{FC4007EF-6CA3-4C60-B106-B85B30641FB9}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{19E3E47A-43F0-479D-87D4-78C57BB91E66}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{FC4007EF-6CA3-4C60-B106-B85B30641FB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{FC4007EF-6CA3-4C60-B106-B85B30641FB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{FC4007EF-6CA3-4C60-B106-B85B30641FB9}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{19E3E47A-43F0-479D-87D4-78C57BB91E66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{19E3E47A-43F0-479D-87D4-78C57BB91E66}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{19E3E47A-43F0-479D-87D4-78C57BB91E66}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{19E3E47A-43F0-479D-87D4-78C57BB91E66}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

‎StackExchange.NET/StackExchange.NET/Clients/StackExchangeClient.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System;
2+
using System.Net;
23
using System.Net.Http;
34

45
namespace StackExchange.NET.Clients
@@ -11,6 +12,10 @@ public partial class StackExchangeClient
1112

1213
public StackExchangeClient(string apiKey)
1314
{
15+
if (string.IsNullOrWhiteSpace(apiKey))
16+
{
17+
throw new Exception($"Api Key cannot be null or empty : {nameof(apiKey)}");
18+
}
1419
_apiKey = apiKey;
1520
_baseApiUrl = $"https://api.stackexchange.com/2.2";
1621
var httpClientHandler = new HttpClientHandler()

‎StackExchange.NET/Tests/AnswerClient.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#region Using Directives
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using NSubstitute;
7+
using NUnit.Framework;
8+
using StackExchange.NET.Clients;
9+
using StackExchange.NET.Interfaces;
10+
using StackExchange.NET.Models;
11+
12+
#endregion
13+
14+
namespace Tests
15+
{
16+
public class AnswerClientTests
17+
{
18+
private BaseResponse<Answer> _baseResponse;
19+
private IAnswers _answers;
20+
private AnswerFilters _answerFilters;
21+
private StackExchangeClient _client;
22+
[SetUp]
23+
public void Setup()
24+
{
25+
_baseResponse = Substitute.For<BaseResponse<Answer>>();
26+
_answers = Substitute.For<IAnswers>();
27+
_answerFilters = Substitute.For<AnswerFilters>();
28+
_client = Substitute.For<StackExchangeClient>("someKey");
29+
}
30+
31+
[Test]
32+
public void GetAllAnswers()
33+
{
34+
_answers.GetAllAnswers(_answerFilters).Returns(_baseResponse);
35+
}
36+
37+
[Test]
38+
public void GetAllAnswersWithNullFilter()
39+
{
40+
Assert.Throws<ArgumentNullException>(() => _client.Answers.GetAllAnswers(null));
41+
}
42+
43+
[Test]
44+
public void GetAnswerByIds()
45+
{
46+
var ids = Substitute.For<ICollection<string>>();
47+
_answers.GetAnswerByIds(ids.ToList(), _answerFilters).Returns(_baseResponse);
48+
}
49+
50+
[Test]
51+
public void GetAnswerByIdsWithNull()
52+
{
53+
Assert.Throws<ArgumentNullException>(() => _client.Answers.GetAnswerByIds(null, _answerFilters));
54+
}
55+
}
56+
}

‎StackExchange.NET/Tests/Tests.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="NSubstitute" Version="4.2.1" />
11+
<PackageReference Include="nunit" Version="3.11.0" />
12+
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\StackExchange.NET\StackExchange.NET.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /