这是使用NUnit和Moq从ASP.NET MVC项目中的一个控制器进行的单元测试:
[Test]
public void Create_job_with_modelstate_errors_fails()
{
var job = new JobDto();
this.controller.ModelState.AddModelError("", "");
ActionResult result = this.controller.Create(job);
this.jobService.Verify(p => p.SaveJob(It.IsAny<JobDto>()), Times.Never());
// some other asserts removed for brevity
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但从维护的角度来看,我认为这一行比它需要的更冗长:
this.postService.Verify(p => p.SavePost(It.IsAny<PostDto>()), Times.Never());
Run Code Online (Sandbox Code Playgroud)
我真正希望能够做的是......
this.postService.VerifyNoMethodsCalled();
Run Code Online (Sandbox Code Playgroud)
...因为我感兴趣的是我的控制器不会调用服务上的任何方法.这可能使用Moq吗?
有谁知道是否可以从Razor TBB中读取页面模板元数据?我正在实现一个使用960网格系统构建的设计并保持我的CT可重用性我希望能够做到这样的事情:
<div class="@Page.Template.Metadata.content_grid">
</div>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会引发空引用异常.
我正在尝试可能或者我需要编写自定义TBB来将此信息添加到包中吗?
我在SDL Tridion 2011 SP 1-1中编写了一些自定义事件代码,它在初始化阶段修改了保存或本地化的组件字段:
var localize = EventSystem.Subscribe<Component, LocalizeEventArgs>(OnComponentLocalize, EventPhases.Initiated);
var save = EventSystem.Subscribe<Component, SaveEventArgs>(OnComponentSave, EventPhases.Initiated);
_subscriptions.Add(localize);
_subscriptions.Add(save);
Run Code Online (Sandbox Code Playgroud)
save事件工作正常,但相同的代码对localize事件不起作用 - 对Component的XML所做的任何更改都将被丢弃.我使用非常简单的代码:
var fields = new ItemFields(component.Content, component.Schema);
var translatedSummary = fields["summary"] as MultiLineTextField;
translatedSummary.Value = translation;
component.Content = fields.ToXml();
Run Code Online (Sandbox Code Playgroud)
我可以得到改变坚持本地化的唯一方法是,如果我在一个帖子做/提交阶段,并做了退房/更新和保存/办理入住手续.这不是很大,因为它需要几秒钟来执行&在本地化Component之后,它在单独的事务中执行.有谁知道更优雅的方式来做到这一点?
我们有一个使用LINQ to SQL的项目,为此我需要重写几个搜索页面以允许客户端选择是否要执行和/或搜索.
我虽然使用PredicateBuilder重做LINQ查询,但我认为这个工作得很好.我实际上有一个包含我的谓词的类,例如:
internal static Expression<Func<Job, bool>> Description(string term)
{
return p => p.Description.Contains(term);
}
Run Code Online (Sandbox Code Playgroud)
要执行搜索,我正在执行此操作(为简洁起见,省略了一些代码):
public Expression<Func<Job, bool>> ToLinqExpression()
{
var predicates = new List<Expression<Func<Job, bool>>>();
// build up predicates here
if (SearchType == SearchType.And)
{
query = PredicateBuilder.True<Job>();
}
else
{
query = PredicateBuilder.False<Job>();
}
foreach (var predicate in predicates)
{
if (SearchType == SearchType.And)
{
query = query.And(predicate);
}
else
{
query = query.Or(predicate);
}
}
return query;
}
Run Code Online (Sandbox Code Playgroud)
虽然我对此感到满意,但我有两个顾虑: …
我刚刚使用SiteEdit 2009 SP2启动了SiteEdit,启用了我的第一个Tridion项目.虽然它对我的大多数组件似乎都运行正常,但每当我在SiteEdit模式下打开页面时,以下消息始终显示在顶部:
蓝图状态:无效设置
有谁知道造成这种情况的原因,以及如何阻止它出现?
我有一个相当简单的Sitecore MVC渲染,其中包含一个标题字段和一个占位符:
<section>
<div class="container">
<h2 class="m-header"><span>@Html.Sitecore().Field("PromoItemsHeader")</span></h2>
<div class="l-section grid">
@Html.Sitecore().Placeholder("PromoItems")
</div>
</div>
</section>
Run Code Online (Sandbox Code Playgroud)
我希望此渲染仅在占位符包含项目时显示在页面编辑模式之外.这似乎应该很简单,但我找不到一个明显/干净的方法.
tridion ×3
c# ×1
linq ×1
linq-to-sql ×1
mocking ×1
moq ×1
sitecore ×1
sitecore-mvc ×1
sitecore7 ×1
siteedit ×1
tridion-2011 ×1
unit-testing ×1