我在代码中有一个返回OrganisationModel的方法。
public virtual OrganisationModel GetCurrentUserOrganisation()
{
var user = DBEntities.AspNetUsers.Find(_userId);
if (user.ActiveOrganisation != null)
{
var org = user.OrganisationUsers.Where(p => p.organisationId ==
user.ActiveOrganisation).Where(p => p.userId == _userId).FirstOrDefault();
if(org != null)
{
if (org.Organisation != null)
{
return org.Organisation.ToModel();
}
return null;
}
else
{
return null;
}
}
return null;
}
我想在使用此方法的类上编写单元测试,我的测试看起来像这样
public void Index()
{
HttpContext.Current = _TestContext;
var _GovernorLogic = new Mock<GovernorLogic>();
var testdata = new OrganisationModel { OrganisationId = 3, OrganisationName = "Test Org" };
var _OrganisationLogic = new Mock<OrganisationLogic>(new UserId { userId = "e58bf834-2c06-4cc0-ac00-231a5e1a9bbc" }) ;
_OrganisationLogic.Setup((OrganisationLogic p) => p.GetCurrentUserOrganisation()).Returns((OrganisationModel model) => testdata).Callback((OrganisationLogic a) => receivedorganisation = a.GetCurrentUserOrganisation());
GovernorsController controller = new GovernorsController(_OrganisationLogic.Object);
ViewResult result = controller.Index() as ViewResult;
Assert.IsNotNull(result);
}
当我运行时,我收到错误告诉我
System.ArgumentException无效的回调。使用0参数设置方法不能使用不同数量的参数调用回调(1)。 Source = Moq StackTrace:在Moq.MethodCallReturn
2.ValidateNumberOfCallbackParameters(MethodInfo callbackMethod) at Moq.MethodCallReturn
2.ValidateReturnDelegate(委托回调)在Moq.MethodCallReturn2.Returns[T](Func
2 valueExpression)
如何测试不接受输入参数但具有返回类型的方法?
我正在尝试测试的控制器操作方法与控制器的构造函数一起显示在下面。
[Inject]
public GovernorsController(GovernorLogic GovernorLogic, OrganisationLogic logic) : base(logic)
{
_GovernorLogic = GovernorLogic;
}
public ActionResult Index()
{
var CurrentGovernors = _GovernorLogic.GetAllCurrentGovernors(Organisation.GetCurrentUserOrganisation().OrganisationId).ToList().Select(p => p.ToViewModel()).ToList();
var ArchivedGovernors = _GovernorLogic.GetAllArchivedGovernors(Organisation.GetCurrentUserOrganisation().OrganisationId).ToList().Select(p => p.ToViewModel()).ToList();
var Model = new GovernorsViewModel
{
CurrentGovernors = CurrentGovernors,
ArchivedGovernors = ArchivedGovernors,
};
return View(Model);
}
GovernorsController继承的基本控制器如下所示:
public class BaseController : Controller
{
private OrganisationLogic _organisationLogic;
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private string _userId;
[Inject]
public BaseController(OrganisationLogic OrganisationLogic)
{
_userId = System.Web.HttpContext.Current.User.Identity.GetUserId();
_organisationLogic = new OrganisationLogic(new UserId {userId= System.Web.HttpContext.Current.User.Identity.GetUserId()});
}
protected OrganisationLogic Organisation
{
get
{
return _organisationLogic;
}
}
public BaseController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
set
{
_userManager = value;
}
}
}
------最小完整示例-------
基本控制器
public class BaseController : Controller
{
private OrganisationLogic _organisationLogic;
private string _userId;
[Inject]
public BaseController(OrganisationLogic OrganisationLogic)
{
_userId = System.Web.HttpContext.Current.User.Identity.GetUserId();
_organisationLogic = new OrganisationLogic(new UserId {userId= System.Web.HttpContext.Current.User.Identity.GetUserId()});
}
[ChildActionOnly]
public ActionResult UserOrganisations()
{
if (User.Identity.IsAuthenticated)
{
var ViewModel = _OrganisationLogic.GetAll().ToList().Select(p=>p.ToViewModel());
return PartialView("_UserOrganisations", ViewModel);
}
return null;
}
}
///The child action in the base controller is use in the layout page to render organisation in a drop down list. It is this that is making use of _OrganisationLogic /////
@Html.Action("UserOrganisations", "Base",new {area = "" })
GovernorsController
public class GovernorsController : OrganisationBaseController
{
[Inject]
public GovernorsController(OrganisationLogic logic) : base(logic)
{
}
// GET: Organisation/Governors
public ActionResult Index()
{
var Model = new SomeModel();
return View(Model);
}
}
OrganisationLogic
public class OrganisationLogic : LogicRepository<OrganisationModel>
{
private string _userId;
public OrganisationLogic(UserId userId)
{
_userId = userId.userId;
}
public override List<OrganisationModel> GetAll()
{
var orgs = DBEntities.Organisations.Where(p=>p.IsDeleted!=true).ToList().Select(p=>p.ToModel()).ToList();
return orgs;
}
}