Skip to content

Instantly share code, notes, and snippets.

@nest-don
Created January 31, 2019 05:53
Show Gist options
  • Save nest-don/4f9d7abb89aaa3945c3f27f3ff722b93 to your computer and use it in GitHub Desktop.
Save nest-don/4f9d7abb89aaa3945c3f27f3ff722b93 to your computer and use it in GitHub Desktop.
public class UnitTests
{
[Fact]
public void TestSearch()
{
// Arrange
var mockRuntime = new Mock<Runtime>(
QueueMode.Server | QueueMode.Client,
180, Enviorenment.Development
) {CallBase = true};
mockRuntime.Setup(x => x.ReceiveSingle<SearchResult>())
.Returns(ResultSingle<SearchResult>.ConvertObject(
GetInput("test-input-a"), new SearchResult()));
mockRuntime.Setup(x => x.ReceiveSingle<SearchResult>())
.Returns(ResultSingle<SearchResult>.ConvertObject(
GetInput("test-input-b"), new SearchResult()));
var serviceProvider = new ServiceCollection()
.AddLogging()
.BuildServiceProvider();
var factory = serviceProvider.GetService<ILoggerFactory>();
var logger = factory.CreateLogger<SearchController>();
var controller = new SearchController(
logger, mockRuntime.Object);
// Act
var result = controller.Get("docker");
// Assert
var okResult = result as JsonResult;
Assert.NotNull(okResult);
Assert.Equal(200, okResult.StatusCode);
Assert.IsType<Result<List<SearchResult>>>(okResult.Value);
}
private string GetInput(string name)
{
string content;
using (FileStream fs = File.Open("data/" + name + ".json", FileMode.Open))
{
using (var reader = new StreamReader(fs))
{
content = reader.ReadToEnd();
}
}
return content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment