Reputation: 25
Hi I have to write unit test to this method:
public static async Task<TradeModel[]> LoadTrades(int limit = 300)
{
string url = "";
if (limit <= 300)
{
url = $"https://api.bitbay.net/rest/trading/transactions/BTC-PLN?limit={ limit }";
}
else
{
}
using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))//exeption
{
if (response.IsSuccessStatusCode)
{
TradeItemModel trade = await response.Content.ReadAsAsync<TradeItemModel>();
return trade.Items;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
And my unit test:
[TestMethod]
public async Task TestMethod1()
{
List<int> numbers = new List<int> { 1, 26, 70, 5 };
List<Type> expected = new List<Type> { typeof(TradeModel[]), typeof(TradeModel[]), typeof(TradeModel[]), typeof(TradeModel[]) };
List<Type> actual = new List<Type>();
for(int i= 0; i < 4; i++)
{
var trades = await TradeProcessor.LoadTrades(numbers[i]);
actual.Add(trades.GetType());
}
CollectionAssert.AreEqual(expected, actual);
}
I don't understand why I get an exeption Object reference not set to an instance of an object. What am I doing wrong? How this test should look like?
Upvotes: 0
Views: 33
Reputation: 1061
I received the same error when implementing a unit test for WPF. In WPF you have to run the control in an Apartment Thread. To do this, I've used NUnit and tagged every unit test class with [Apartment(ApartmentState.STA)]
. Other Unit Test Frameworks should have similar ways of enforcing this Threading state...
[Apartment(ApartmentState.STA)]
public class ByteTests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestValues()
{
}
}
Upvotes: 1