-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from 1RedOne/addTestsForBitcoinClient
π #HackTober - add tests for bitcoin prices π
- Loading branch information
Showing
5 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
ο»Ώusing System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace UtilityBelt.Interfaces | ||
{ | ||
public interface IWebClient : IDisposable | ||
{ | ||
// Required methods (subset of `System.Net.WebClient` methods). | ||
byte[] DownloadData(Uri address); | ||
|
||
byte[] UploadData(Uri address, byte[] data); | ||
|
||
string DownloadString(string address); | ||
} | ||
|
||
public interface IWebClientFactory | ||
{ | ||
IWebClient Create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
ο»Ώusing System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using UtilityBelt.Interfaces; | ||
|
||
namespace UtilityBelt.Models | ||
{ | ||
/// <summary> | ||
/// System web client. | ||
/// </summary> | ||
public class SystemWebClient : WebClient, IWebClient | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// System web client factory. | ||
/// </summary> | ||
public class SystemWebClientFactory : IWebClientFactory | ||
{ | ||
#region IWebClientFactory implementation | ||
|
||
public IWebClient Create() | ||
{ | ||
return new SystemWebClient(); | ||
} | ||
|
||
#endregion IWebClientFactory implementation | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using System; | ||
using System.Net; | ||
using System.Text.Json; | ||
using UtilityBelt; | ||
using UtilityBelt.Interfaces; | ||
using UtilityBelt.Models; | ||
using UtilityBelt.Utilities; | ||
using Xunit; | ||
|
||
namespace UtilityBeltxUnitTests | ||
{ | ||
public class BitCoinPricesTests | ||
{ | ||
private readonly Mock<IWebClient> webClient = new Mock<IWebClient>(); | ||
|
||
public BitCoinPricesTests() | ||
{ | ||
var mockBitCoinResult = getBitCoinPrice(); | ||
var jsonresult = JsonSerializer.Serialize(mockBitCoinResult); | ||
this.webClient.Setup(x => x.DownloadString(It.IsAny<string>())).Returns(jsonresult); | ||
} | ||
|
||
[Fact] | ||
public void BitCoinPricesReturnsPrice() | ||
{ | ||
var bitCoinClient = new TestableBitcoinPrices(webClient.Object); | ||
bitCoinClient.Run(); | ||
this.webClient.Verify(x => x.DownloadString(It.IsAny<string>()), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void BitCoinPricesDoesNotThrowsIfNoResult() | ||
{ | ||
this.webClient.Setup(x => x.DownloadString(It.IsAny<string>())).Returns(""); | ||
|
||
var bitCoinClient = new TestableBitcoinPrices(webClient.Object); | ||
bitCoinClient.Run(); | ||
this.webClient.Verify(x => x.DownloadString(It.IsAny<string>()), Times.Once); | ||
} | ||
|
||
private BitcoinPrice getBitCoinPrice() | ||
{ | ||
var mockBitCoinTime = new BitcoinTime(); | ||
mockBitCoinTime.Updated = "Just now"; | ||
|
||
var mockbitCoinBpi = new BitcoinBpi() | ||
{ | ||
USD = new Currency() | ||
{ | ||
Rate = "1000" | ||
} | ||
}; | ||
|
||
var mockBitCoinPrice = new BitcoinPrice | ||
{ | ||
Bpi = mockbitCoinBpi, | ||
Time = mockBitCoinTime | ||
}; | ||
|
||
return mockBitCoinPrice; | ||
} | ||
|
||
private class TestableBitcoinPrices : BitcoinPrices | ||
{ | ||
public IWebClient webClient { get; set; } | ||
|
||
public TestableBitcoinPrices(IWebClient webClient) : base() | ||
{ | ||
this.webClient = webClient; | ||
} | ||
|
||
protected override IWebClient GetWebClient() => this.webClient; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters