ありがとうございます。
public interface ICustomSearchIndexClient
{
DocumentSearchResult<T> Search<T>(string searchTerm, SearchParameters parameters) where T : class;
}
public class CustomSearchIndexClient : ICustomSearchIndexClient
{
private readonly SearchIndexClient _searchIndexClient;
public CustomSearchIndexClient(string searchServiceName, string indexName, string apiKey)
{
_searchIndexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(apiKey));
}
public DocumentSearchResult<T> Search<T>(string searchTerm, SearchParameters parameters) where T: class
{
return _searchIndexClient.Documents.Search<T>(searchTerm, parameters);
}
}
このような変更されたビジネス・ロジック:
コンストラクタ:
public CustomSearchService(string serviceName, string apiKey, string indexName, ICustomSearchIndexClient customSearchIndexClient)
{
_serviceName = serviceName;
_apiKey = apiKey;
_indexName = indexName;
_customSearchIndexClient = customSearchIndexClient;
}
検索方法:
私はこのようなSearchIndexClientのラッパーを作成し
:仕事の下には周りの私の問題を解決しました
public DocumentSearchResult<CustomSearchResult> Search(string search)
{
return _customSearchIndexClient.Search<CustomSearchResult>(string.IsNullOrEmpty(search) ? "*" : search, null)
}
このような私のユニットテストを変更:ラッパーICustomSearchIndexがCustomSearchServiceビジネス・ロジック内に注入される
[TestCategory("UnitTest")]
[TestMethod]
public void SearchTest()
{
//Arrange
var expectedResponse = new DocumentSearchResult<Models.CustomSearchResult> { Count = 1, Results = <instantiate your custom model here>, Facets = < instantiate your custom model here > };
var searchIndexClient = new Mock<ICustomSearchIndexClient>();
searchIndexClient.Setup(r => r.Search<Models.CustomSearchResult>(It.IsAny<string>(), null)).Returns(expectedResponse);
var business = new CustomSearchService("serviceName", "apiKey", "indexname", searchIndexClient.Object);
//Act
var result = business.Search("search term");
//Assert
Assert.IsNotNull(result, "Business logic method returned NULL");
}
はninject使用:
Bind<ICustomSearchIndexClient>().To<CustomSearchIndexClient>();
Bind<ICustomSearchService>().To<CustomSearchService>()
.WithConstructorArgument("serviceName", ConfigurationManager.AppSettings["SearchServiceName"])
.WithConstructorArgument("apiKey", ConfigurationManager.AppSettings["SearchServiceApiKey"])
.WithConstructorArgument("indexName", ConfigurationManager.AppSettings["IndexName"]);
[ 'Search'(https://docs.microsoft.com/ Microsoft Corporation、その関連会社及びこれらの者の供給者は、直接的、間接的、偶発的、結果的損害、逸失利益、懲罰的損害、または特別損害を含む全ての損害に対して、状況のいかんを問わず一切責任を負いません。拡張メソッドがアクセスするものをチェックしてから、拡張メソッドが完了するようにモックする必要があります。 – Nkosi
Microsoft FakesまたはPose(https://github.com/tonerdo/pose)を使用して拡張メソッドをシムにする – zaitsman
*いくつかのアドバイス:*あなたが所有/管理していないものを嘲笑しないでください。第三者の依存関係からコードを切り離すように制御する抽象概念の背後にそれらをカプセル化します。これにより、コードを単独でテストする際の柔軟性が向上します。 – Nkosi