我正在尝试对使用成员资格提供程序更新用户详细信息的控制器操作进行单元测试.我使用的Moq到目前为止一直很容易使用.
问题是我似乎无法模拟对不返回任何内容的方法的调用.
<TestMethod()> _
Public Sub Can_Update_User()
' Arrange
_membershipService.Setup(Function(x) x.UpdateUser(It.IsAny(Of MembershipUser)))
Dim controller As New UsersController(_membershipService.Object, _roleProvider.Object, _supportWorksService.Object, _portalClientService.Object)
' Act
Dim result As ViewResult = controller.Edit("testUser", New FormCollection)
' Assert
Assert.AreEqual("Index", result.ViewName)
End Sub
Run Code Online (Sandbox Code Playgroud)
模拟的成员资格服务的设置将无法编译,错误是:
重载决策失败,因为无法使用以下参数调用可访问的"Setup":
'公共函数设置(Of TResult)(表达式为System.Linq.Expressions.Expression(Of System.Func(Of Services.IMembershipService,TResult)))作为Moq.Language.Flow.ISetup(Of Services.IMembershipService,TResult)' :表达式不会产生值.
'公共函数设置(Of TResult)(表达式为System.Linq.Expressions.Expression(Of System.Func(Of Services.IMembershipService,TResult)))作为Moq.Language.Flow.ISetup(Of Services.IMembershipService,TResult)' :无法从这些参数推断出类型参数的数据类型.明确指定数据类型可能会更正此错误.
'公共函数设置(表达式为System.Linq.Expressions.Expression(Of System.Action(Of Services.IMembershipService)))作为Moq.Language.Flow.ISetup(Of Services.IMembershipService)':表达式不生成值.
我错过了什么?我是否必须创建一个假类,而不是在我的班级有一个我想要调用它的方法时使用Moq?
编辑:
好吧,一点点阅读表明这是由于使用Function()必须有结果的lambda在VB中表达的方式.
有没有人为此找到了解决办法,还是我不得不放弃Moq伪造方法?
我正在尝试创建一个自定义路由来处理具有多个参数的特定网址.我已经向global.asax添加了一个新的新路由定义,但是当我尝试导航到该操作时,我收到错误消息"找不到与名为'Home'的控制器匹配的类型."
谁能告诉我哪里出错了?
Global.asax中:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "HomeDetails",
routeTemplate: "Home/Detail/{articleId}/{articleVersion}",
defaults: new { controller = "Home", action = "Detail", articleId = "0", articleVersion = "0.0" }
);
//routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public class HomeController : Controller
{
public ViewResult Index()
{
return …Run Code Online (Sandbox Code Playgroud) 因此,我一直在处理我的项目,然后在某个时候 Visual Studio 决定我还没有克隆我的存储库并希望再次克隆。
如果我直接从文件 | 打开我的解决方案 打开| 项目/解决方案然后团队资源管理器似乎不知道它是团队服务项目并且缺少选项。
有什么办法可以重新连接这些吗?我一直不得不删除本地存储库并从远程重新克隆它。如果我在本地提交了需要推动的更改,那么这毫无帮助。
我在我的ASP.NET MVC控制器上使用自定义授权过滤器,如果用户在特定操作上失败,则会将用户重定向到登录屏幕以外的URL.
这对于返回视图的操作是可以的,但是我的许多操作都返回其他结果类型,例如PartialResult或JsonResult.
我当前的过滤器如下所示:
<AuthorizeWithRedirect(角色:="ServerAccess",控制器:="主页",操作:="未经授权")>
这表示如果用户不在ServerAccess角色中,则应将其重定向到/ Home/Unauthorized /
我很好奇其他人是如何处理这个的?当您考虑仅由客户端脚本AJAX调用调用的操作数时,这似乎特别成问题./ Home/Unauthorized/action如何知道调用者是否打算接收视图,部分视图,json,内容等?
我正在使用TFS Power Tools中的警报资源管理器在TFS 2010中设置我的电子邮件警报.
我想在构建失败或部分成功时收到电子邮件.过滤器似乎太基本无法实现这一点.
我按如下方式创建了过滤器
"TeamProject"="我的项目"和"StatusCode"='失败'或"StatusCode"='PartiallySucceeded'
但显然,只要任何项目的构建部分成功,它就会向我发送一封电子邮件.我真正想要的过滤器是
"TeamProject"="我的项目"AND("StatusCode"='失败'或"StatusCode"='PartiallySucceeded')
有没有办法实现这个目标?我可以用这样的方式输入值,使其读为IN('this'或'that')吗?
如果我能提供帮助,我真的不想为我感兴趣的每个状态创建单独的电子邮件规则.
我正在使用MSF Agile 5.0项目模板.
我想修改Product Backlog查询以包含没有父项的任务.该查询目前仅显示用户故事.
我没有看到要查询的字段,例如父项ID或类似项.
有没有人这样做过?
我刚刚开始使用RavenDB,到目前为止我还喜欢它.然而,我仍然坚持如何单元测试与之交互的控制器操作.
我发现的所有问题/文章都是这样的:单元测试RavenDb查询告诉我应该在内存中使用RavenDB而不是嘲笑它但是我找不到一个如何完成的实例.
例如,我有一个控制器操作将员工添加到数据库(是的,它过于简化,但我不想让问题复杂化)
public class EmployeesController : Controller
{
IDocumentStore _documentStore;
private IDocumentSession _session;
public EmployeesController(IDocumentStore documentStore)
{
this._documentStore = documentStore;
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
_session = _documentStore.OpenSession("StaffDirectory");
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (_session != null && filterContext.Exception == null) {
_session.SaveChanges();
_session.Dispose();
}
}
[HttpGet]
public ViewResult Create()
{
return View();
}
[HttpPost]
public RedirectToRouteResult Create(Employee emp)
{
ValidateModel(emp);
_session.Store(emp);
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
如何在单元测试中验证添加到数据库的内容?有没有人在MVC应用程序中有任何涉及RavenDb的单元测试的例子?
我正在使用MSTest,如果这很重要,但我很乐意尝试从其他框架中翻译测试.
谢谢.
编辑
好吧,我的测试初始化创建了注入控制器构造函数的文档存储,但是当我运行测试时,OnActionExecuting事件没有运行,所以没有会话要使用,测试失败并带有空引用异常.
[TestClass]
public class …Run Code Online (Sandbox Code Playgroud) 我已将用于MVC的 Jeremiah Clark的CheckBoxList Helper翻译成我的VB.Net项目但是当我尝试在我的视图中使用该方法时,我得到了错误
'CheckBoxList' is not a member of 'System.Web.Mvc.HtmlHelper(Of Attenda.Stargate.Web.UserRolesViewModel)'.
Run Code Online (Sandbox Code Playgroud)
谁能告诉我哪里出错了?
助手模块:
Imports System.Runtime.CompilerServices
Public Module InputExtensions
<Extension()> _
Public Function CheckBoxList(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal listInfo As List(Of ListItem)) As String
Return htmlHelper.CheckBoxList(name, listInfo, DirectCast(Nothing, IDictionary(Of String, Object)))
End Function
<Extension()> _
Public Function CheckBoxList(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal listInfo As List(Of ListItem), ByVal htmlAttributes As Object) As String
Return htmlHelper.CheckBoxList(name, listInfo, DirectCast(New RouteValueDictionary(htmlAttributes), IDictionary(Of String, …Run Code Online (Sandbox Code Playgroud) 我已经安装了OpsHub VSO Migration Utilty,我正在尝试迁移一个最小的项目作为测试.
在VSO中创建了一个新的匹配项目后,迁移验证在模板不匹配时失败.
现有的TFS 2010项目基于MSF for Agile Software Development v5.0模板,新的VSO项目是MSF for Agile Software Development 2013.3
据我所知,这些模板都没有定制过.
对于每个工作项类型,不匹配的字段为:
该实用程序的已安装版本为v1.1.0.005
我们将非常感激地提供任何帮助.
我有一个自定义对象的通用列表,并希望将该列表减少到特定属性值不在排除列表中的对象.
我尝试过以下方法:
Private Sub LoadAddIns()
// Get add-in templates
Dim addIns = GetTemplates(TemplateTypes.AddIn)
// Get the current document
Dim sectionId As String = CStr(Request.QueryString("sectionId"))
Dim docId As Integer = CInt(Split(sectionId, ":")(0))
Dim manual = GetTempManual(docId)
Dim content As XElement = manual.ManualContent
// Find which templates have been used to create this document.
Dim usedTemplates = (From t In content.<header>.<templates>.<template> _
Select CInt(t.<id>.Value)).ToList
// Exclude add-ins that have already been used.
If usedTemplates IsNot Nothing Then
addIns = addIns.Where(Function(a) usedTemplates.Contains(a.TemplateID) = …Run Code Online (Sandbox Code Playgroud) 我有一个项目集合,每个项目都有一组关系.我有一个项目列表,项目可以与之关系.
我可以找到所有具有特定关系的项目,但我现在想要找到所有与我的任何组没有关系的项目.
通过这样做,我可以找到与任何组有关系的项目:
Dim groupIds as List(of Integer) = (From g In cmdbGroups Select g.ID).ToList
Dim haveGroup = (From item In items _
Where item.Relationships.Any(Function(r) groupIds.Contains(r.TargetID)) _
Select item).ToList
Run Code Online (Sandbox Code Playgroud)
如何查找与任何组无关的所有项目?
asp.net-mvc ×4
vb.net ×3
tfs2010 ×2
unit-testing ×2
ajax ×1
azure-devops ×1
c# ×1
git ×1
html-helper ×1
json ×1
linq ×1
moq ×1
mstest ×1
opshub ×1
ravendb ×1
routing ×1
tfs-alerts ×1
workitem ×1