Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用构造函数注入对asp.net MVC控制器进行单元测试_C#_Asp.net Mvc_Unit Testing_Automated Tests_Integration Testing - Fatal编程技术网

C# 使用构造函数注入对asp.net MVC控制器进行单元测试

C# 使用构造函数注入对asp.net MVC控制器进行单元测试,c#,asp.net-mvc,unit-testing,automated-tests,integration-testing,C#,Asp.net Mvc,Unit Testing,Automated Tests,Integration Testing,如何设置测试项目,以便对控制器进行单元测试, 我无法从测试方法调用控制器操作,因为它需要依赖接口对象。 我不想实现任何模拟存储库。 我希望使用与集成测试相同的现有数据库存储库和可用的业务逻辑 我的项目结构如下: MVC.Web MVC.BL(接口和实现) 存储库(接口和实现) 存储库示例: public class UserRepository : IUserRepository { private readonly IBaseUOW<DBEntitiesEntities>

如何设置测试项目,以便对控制器进行单元测试, 我无法从测试方法调用控制器操作,因为它需要依赖接口对象。 我不想实现任何模拟存储库。 我希望使用与集成测试相同的现有数据库存储库和可用的业务逻辑

我的项目结构如下:

  • MVC.Web
  • MVC.BL(接口和实现)
  • 存储库(接口和实现)
存储库示例:

public class UserRepository : IUserRepository
{
    private readonly IBaseUOW<DBEntitiesEntities> _dbContext;
    private readonly IBaseRepository<User> _userRepository;

    public UserRepository(IBaseUOW<MoynoECTNewEntities> dbContext)
    {
        _dbContext = dbContext;
        _userRepository = dbContext.GetRepository<User>();
    }

    public UserResponse GetUserList()
    {
        var response = new UserResponse {Acknowledge = AcknowledgeType.Success};

        var typeList = _userRepository.Get().OrderBy(x => x.UserName).ToList();

        response.UserList = typeList.ToList();

        return response;
    }
}
控制器示例:

public class UserBL : IUserBL
{
    public UserResponse GetUserList()
    {
        var response = new UserResponse();

        var userList = _userRepository.GetUserList();
        if (userList != null && userList.UserList.Count > 0)
        {
            response.UserList = userList.UserList.ToList();
        }
        else
        {
            response.Message = "No Data Found";
            response.Acknowledge = AcknowledgeType.Failure;
        }

        return response;
    }
}
public class AdminController : BaseController
{
    public readonly IUserBL _userBl;
    public readonly IUserRoleBL _userRoleBl;

    public AdminController(IUserRoleBL userRoleBl, IUserBL userBl)
    {
        _userRoleBl = userRoleBl;
        _userBl = userBl;
    }

    public ActionResult GetUserList()
    {
        var response = _userBl.GetUserList();
        return View("_GridViewUser", response.UserList);
    }
}
我的单元测试方法示例:

[TestClass]
public class AdminControllerTest
{

    [TestMethod]
    public void Check_UserMaster_ViewName()
    {
       //assemple
        var controller = // how to call controller ?
       // act
        var result= // controller.GetUserList() as ViewResult;
       // assert
        Assert.AreEqual("UserMaster", result.ViewName);
    }
}
我不想实现模拟存储库,而是直接想使用现有的数据库存储库

我认为这是错误的方法,因为您正在对类进行单元测试,这意味着所有依赖项都不应该影响测试的有效性,但这是您的要求

只需在测试中传入一个“真实”存储库的实例

var userRoleBL = new UserBL();
var userBL = new UserRoleBL();
var controller = new AdminController(userRoleBL, userBL);
// test the controller
我不想实现模拟存储库,而是直接想使用现有的数据库存储库

我认为这是错误的方法,因为您正在对类进行单元测试,这意味着所有依赖项都不应该影响测试的有效性,但这是您的要求

只需在测试中传入一个“真实”存储库的实例

var userRoleBL = new UserBL();
var userBL = new UserRoleBL();
var controller = new AdminController(userRoleBL, userBL);
// test the controller
我不想实现模拟存储库,而是直接想使用现有的数据库存储库

我认为这是错误的方法,因为您正在对类进行单元测试,这意味着所有依赖项都不应该影响测试的有效性,但这是您的要求

只需在测试中传入一个“真实”存储库的实例

var userRoleBL = new UserBL();
var userBL = new UserRoleBL();
var controller = new AdminController(userRoleBL, userBL);
// test the controller
我不想实现模拟存储库,而是直接想使用现有的数据库存储库

我认为这是错误的方法,因为您正在对类进行单元测试,这意味着所有依赖项都不应该影响测试的有效性,但这是您的要求

只需在测试中传入一个“真实”存储库的实例

var userRoleBL = new UserBL();
var userBL = new UserRoleBL();
var controller = new AdminController(userRoleBL, userBL);
// test the controller

正如@D Stanley所说,您应该只测试控制器是否返回了适当的结果,而不是测试其他层、数据库等是否正常工作

但是,要在单元测试中创建控制器,请使用:

var controller = new AdminController(any arguments/contexts here);

正如@D Stanley所说,您应该只测试控制器是否返回了适当的结果,而不是测试其他层、数据库等是否正常工作

但是,要在单元测试中创建控制器,请使用:

var controller = new AdminController(any arguments/contexts here);

正如@D Stanley所说,您应该只测试控制器是否返回了适当的结果,而不是测试其他层、数据库等是否正常工作

但是,要在单元测试中创建控制器,请使用:

var controller = new AdminController(any arguments/contexts here);

正如@D Stanley所说,您应该只测试控制器是否返回了适当的结果,而不是测试其他层、数据库等是否正常工作

但是,要在单元测试中创建控制器,请使用:

var controller = new AdminController(any arguments/contexts here);

@D Stanley,很抱歉,我没有在上面提供正确的业务逻辑构造函数,因为构造函数重载了存储库接口,在这种情况下,该怎么办?因此,将
UserRepository
传递给您的
UserBL
。同样的原则-如果决定不模拟,则需要包含所有依赖项。如果这些依赖项也有依赖项,则需要包含它们。请注意,如果您模拟
IUserBL
,则不必引用存储库:)@D Stanley,很抱歉,我没有在上面提供正确的业务逻辑构造函数,因为存储库接口超载,在这种情况下,该怎么办?那么将
UserRepository
传递给您的
UserBL
。同样的原则-如果决定不模拟,则需要包含所有依赖项。如果这些依赖项也有依赖项,则需要包含它们。请注意,如果您模拟
IUserBL
,则不必引用存储库:)@D Stanley,很抱歉,我没有在上面提供正确的业务逻辑构造函数,因为存储库接口超载,在这种情况下,该怎么办?那么将
UserRepository
传递给您的
UserBL
。同样的原则-如果决定不模拟,则需要包含所有依赖项。如果这些依赖项也有依赖项,则需要包含它们。请注意,如果您模拟
IUserBL
,则不必引用存储库:)@D Stanley,很抱歉,我没有在上面提供正确的业务逻辑构造函数,因为存储库接口超载,在这种情况下,该怎么办?那么将
UserRepository
传递给您的
UserBL
。同样的原则-如果决定不模拟,则需要包含所有依赖项。如果这些依赖项也有依赖项,则需要包含它们。请注意,如果模拟
IUserBL
,则不必引用存储库:)