Uygulanan bir DbContext almak için başka bir yöntem çağrıştıran bir async yöntemi için Microsoft Fakes kullanıyorum. Çünkü async yönteminin içinde çağrılan yönteme ihtiyaç duyulurken, veri tabanı bağlantı dizesi Birim Testinde sağlanmamıştır. Shim, yalnızca bağlantı dizesini kullanan yöntemi atlamakla kalmaz, ayrıca özelleştirilebilir bir DbContext döndürür.Microsoft Fakes, Shim Async Task yöntemini nasıl kullanılır?
public async Task<AccountDataDataContext> GetAccountDataInstance(int accountId)
{
var account = await this.Accounts.FindAsync(accountId);
return AccountDataDataContext.GetInstance(account.AccountDataConnectionString);
}
Ancak, Shim zaman uyumsuz yöntemi ile aşina değilim:
İşte aysnc yöntem uygulamasıdır.
ConfigurationEntities.Fakes.ShimConfigurationDataContext.AllInstances.GetAccountDataInstanceInt32NullableOfInt32 = (x, y, z) => new Task<AccountDataEntities.AccountDataDataContext>(() =>
{
return new SampleContext();// This is the fake context I created for replacing the AccountDataDataContext.
});
Ve SampleContext AccountDataDataContext uyguluyor şöyle: Ne bu benziyordu
Aşağıdapublic class SampleContext: AccountDataDataContext
{
public SampleContext()
{
this.Samples = new TestDbSet<Sample>();
var data = new AccountDataRepository();
foreach (var item in data.GetFakeSamples())
{
this.Samples.Add(item);
}
}
}
test durumu için kod parçası olduğu: Sonuç olarak
[TestMethod]
public async Task SampleTest()
{
using (ShimsContext.Create())
{
//Arrange
SamplesController controller = ArrangeHelper(1);// This invokes the Shim code pasted in the second block and returns SamplesController object in this test class
var accountId = 1;
var serviceId = 2;
//Act
var response = await controller.GetSamples(accountId, serviceId);// The async method is invoked in the GetSamples(int32, int32) method.
var result = response.ToList();
//Assert
Assert.AreEqual(1, result.Count);
Assert.AreEqual("body 2", result[0].Body);
}
}
, Benim sınav durumum sonsuza dek sürüyor. Sanırım Shim lamdas ifadesini tamamen yanlış yazabilirim.
Herhangi bir öneriniz var mı? Teşekkür ederim.
Görev Yapıcısı'nı bilmek güzel, bununla ilgili bazı özellikleri kontrol ediyorum ve haklısınız. İş istasyonuma geldiğimde yarın Task.FromResult() dosyasını deneyeceğim. Teşekkür ederim. –