Mocking a return result set from SearchAsync where SearchResult only has an Internal Constructor #422
Replies: 1 comment
|
With the current public API, there is no supported way to construct a realistic The clean unit-testing boundary is to wrap NRedisStack in an application-owned interface and return your own result type: public interface IProductSearch
{
Task<IReadOnlyList<ProductHit>> SearchAsync(string query);
}
public sealed class RedisProductSearch : IProductSearch
{
private readonly IDatabase _db;
public RedisProductSearch(IDatabase db) => _db = db;
public async Task<IReadOnlyList<ProductHit>> SearchAsync(string query)
{
var result = await _db.FT().SearchAsync("products", new Query(query));
return result.Documents.Select(MapDocument).ToList();
}
}Mock If direct construction is needed broadly, the library would need an explicit public factory/test builder; making the parsing constructor public would expose protocol-specific invariants that callers should not have to reproduce. |
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to mock Search/SearchAsync from the ISearchCommandsAsync/ISearchCommands/FT areas and am running into a problem when returning a resultset as the class SearchResult only has an internal constructor. What is the best method in returning a result set when mocking a Search done through IDatabase.FT().Search / IDatabase.FT().SearchAsync ?
All reactions