【发布时间】:2021-01-10 04:55:16
【问题描述】:
我正在测试我的 viewModel,它包含一个来自依赖注入的方法,但没有进入该方法,老实说,我不知道我做错了什么。
我的测试。
[TestCase("5")]
[TestCase("10")]
[TestCase("-1")]
[TestCase("0")]
[TestCase("100")]
//[TearDown]
[Test]
public async Task Deve_Obter_A_Lista_De_Herois_Dado_A_Quantidade_Menor_Ou_Igual_A_100_E_Maior_Que_Zero(string quantidadeHerois)
{
//Arrang
var mockRepo = new Mock<IHeroes>();
ListHeroesViewViewModel listHeroesViewViewModel = new ListHeroesViewViewModel(null, mockRepo.Object);
//action
await listHeroesViewViewModel.GetHeroes(quantidadeHerois);
//assert
if(quantidadeHerois.Equals("5"))
Assert.AreEqual(5, listHeroesViewViewModel.Herois.Count);
else if (quantidadeHerois.Equals("10"))
Assert.AreEqual(10, listHeroesViewViewModel.Herois.Count);
else
Assert.AreEqual(100, listHeroesViewViewModel.Herois.Count);
}
我的视图模型
public async Task GetHeroes(string limit)
{
try
{
IsBusy = true;
///
////in this point it does not enter the method and returns null////
///
var heroes = await _heroes.GetHeroes(limit);
if (heroes.data != null)
Herois = new ObservableCollection<Result>(heroes.data.results);
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Atenção", $"Error:{ex.Message}", "Ok");
}
finally
{
IsBusy = false;
}
}
构造函数视图模型
public ListHeroesViewViewModel(INavigationService navigationService, IHeroes heroes) : base(navigationService)
{
_navigation = navigationService;
//
///here he receives the dependency caused by mock.object
//
_heroes = heroes;
Title = "Heroes";
}
我不知道我做错了什么,感谢您理解这是我的第一次测试
【问题讨论】:
-
您没有设置
GetHeroes方法。如果这是您的第一次测试,我建议您阅读更多关于模拟的内容。看起来您正在使用起订量,所以从这里开始:github.com/moq/moq4
标签: c# unit-testing xamarin.forms nunit