我怎么能模拟这段代码?
How could I Mock this code?
我想开始模拟一些代码,但我不确定该怎么做。我已经阅读了一些教程,但我无法将其应用到我的代码中(这背后可能是有原因的)。
我正在使用 NUnit 和最小起订量。
(我已经删除了所有其他方法,所以我只能给你看一个)。
Api接口:
public interface IApiRequest
{
Task<T> ExecuteAsync<T>(RestRequest request);
}
我想模拟的方法:
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
var api = new ApiRequest();
return await api.ExecuteAsync<UpcomingMovies>(request);
}
我不确定如何模拟这个。
更新:
现在这是一个有效的测试吗?
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(new UpcomingMovies()));
如果你想模拟 ExecutAsync
方法,你可以这样做:
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(/** whatever movies **/));
如果您想针对特定请求进行模拟,请将 It.IsAny<RestRequest>()
替换为对您的请求的引用。
为了有效地测试你的 class 你需要这样的东西:
public class MyClass
{
public MyClass(IApiRequest api)
{
this.api = api;
}
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
return await api.ExecuteAsync<UpcomingMovies>(request);
}
}
测试
[Test]
public async Task MyTest()
{
var expectedMovies = new UpcomingMovies(); // or whatever movies you need
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(expectedMovies));
var myClass = new MyClass(mock.Object);
var result = await myClass.GetUpcomingMovies(1);
Assert.IsTrue(expectedMovies == result);
}
我想开始模拟一些代码,但我不确定该怎么做。我已经阅读了一些教程,但我无法将其应用到我的代码中(这背后可能是有原因的)。 我正在使用 NUnit 和最小起订量。 (我已经删除了所有其他方法,所以我只能给你看一个)。
Api接口:
public interface IApiRequest
{
Task<T> ExecuteAsync<T>(RestRequest request);
}
我想模拟的方法:
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
var api = new ApiRequest();
return await api.ExecuteAsync<UpcomingMovies>(request);
}
我不确定如何模拟这个。
更新:
现在这是一个有效的测试吗?
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(new UpcomingMovies()));
如果你想模拟 ExecutAsync
方法,你可以这样做:
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(/** whatever movies **/));
如果您想针对特定请求进行模拟,请将 It.IsAny<RestRequest>()
替换为对您的请求的引用。
为了有效地测试你的 class 你需要这样的东西:
public class MyClass
{
public MyClass(IApiRequest api)
{
this.api = api;
}
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
return await api.ExecuteAsync<UpcomingMovies>(request);
}
}
测试
[Test]
public async Task MyTest()
{
var expectedMovies = new UpcomingMovies(); // or whatever movies you need
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(expectedMovies));
var myClass = new MyClass(mock.Object);
var result = await myClass.GetUpcomingMovies(1);
Assert.IsTrue(expectedMovies == result);
}