我正在开发一个从API端点获取数据的Web项目。 API层位于底部的服务层和存储库层之上。我已经为服务和API层编写了单元测试。我正在使用Moq框架来模拟依赖项。 现在我想测试MVC控制器。我正在使用从HttpClient派生的Request Manager类来从API端点获取数据。那么我该如何测试这个控制器呢。我已经编写了单元测试,但测试是直接从我的数据库中获取数据。
public class UserController : Controller
{
private RequestManager requestManager = new RequestManager();
// GET: User
public async Task<ActionResult> Index()
{
List<UserViewModel> allUsers = await requestManager.GetUsers();
if(allUsers == null)
{
throw new HttpException(404, "No Users Found");
}
return View(allUsers);
}
}
public class UserControllerTest
{
public UserController controller;
[OneTimeSetUp]
public void InIt()
{
controller = new UserController();
}
[Test]
public async Task TestIndex()
{
var view = await controller.Index() as ActionResult;
Assert.That(view is ViewResult);
Assert.That(view.Model is List<UserViewModel>);
}
}
答案 0 :(得分:0)
试试这个..
int data[] = { 15, 10, 5 };
const char *pad = "";
for (int i = 0; i < 3; i++)
{
printf("%s%d", pad, data[i]);
pad = " ";
}
putchar('\n');
答案 1 :(得分:0)
你应该解耦控制器和经理。从<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
提取接口并将其注入控制器。这应该不是问题,Ioc容器可以为你做到这一点。
RequestManager
然后在您的单元测试中,您可以模拟IRequestManager。
public class UserController : Controller
{
private RequestManager _requestManager;
public UserController(IRequestManager requestManager)
{
_requestManager = requestManager;
}
// GET: User
public async Task<ActionResult> Index()
{
List<UserViewModel> allUsers = await _requestManager.GetUsers();
if(allUsers == null)
{
throw new HttpException(404, "No Users Found");
}
return View(allUsers);
}
}