我有一个预先存在的界面......
public interface ISomeInterface
{
void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)
并且我使用mixin扩展了这个表面...
public static class SomeInterfaceExtensions
{
public static void AnotherMethod(this ISomeInterface someInterface)
{
// Implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个叫这个我要测试的课程...
public class Caller
{
private readonly ISomeInterface someInterface;
public Caller(ISomeInterface someInterface)
{
this.someInterface = someInterface;
}
public void Main()
{
someInterface.AnotherMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
和测试,我想模拟界面并验证对扩展方法的调用...
[Test]
public void Main_BasicCall_CallsAnotherMethod()
{
// Arrange
var someInterfaceMock = new Mock<ISomeInterface>();
someInterfaceMock.Setup(x => x.AnotherMethod()).Verifiable();
var caller = new Caller(someInterfaceMock.Object);
// Act
caller.Main();
// Assert
someInterfaceMock.Verify();
}
Run Code Online (Sandbox Code Playgroud)
然而,运行此测试会产生异常......
System.ArgumentException: …
Run Code Online (Sandbox Code Playgroud) 我必须使用JavaScript检查一些字符串,但区分大小写会导致问题.例如
if('abc'=='ABC')
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
尽管单词的含义相同,但它不会进入if循环.我也不能使用tolower条款,因为我不知道数据如何来它对于ex意味着:
if('aBc'=='abC')
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
如果可以通过jquery完成,如何为此编写JS函数.
如果公钥标记在旧版本上为空并且在较新版本上设置,是否可以在引用的程序集的不同版本之间执行程序集绑定重定向?
例如,我有两个组件......
System.Web.Mvc,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null
和
System.Web.Mvc,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35
以下程序集绑定重定向是否应该在Asp.Net web.config中工作...
System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Run Code Online (Sandbox Code Playgroud) 有没有办法将使用TestCase的泛型类型传递给NUnit中的测试?
这是我想做的但语法不正确...
[Test]
[TestCase<IMyInterface, MyConcreteClass>]
public void MyMethod_GenericCall_MakesGenericCall<TInterface, TConcreteClass>()
{
// Arrange
// Act
var response = MyClassUnderTest.MyMethod<TInterface>();
// Assert
Assert.IsInstanceOf<TConcreteClass>(response);
}
Run Code Online (Sandbox Code Playgroud)
或者如果没有,实现相同功能的最佳方法是什么(显然我在实际代码中会有多个TestCases)?
用另一个例子更新......
这是传递单个泛型类型的另一个示例...
[Test]
[TestCase<MyClass>("Some response")]
public void MyMethod_GenericCall_MakesGenericCall<T>(string expectedResponse)
{
// Arrange
// Act
var response = MyClassUnderTest.MyMethod<T>();
// Assert
Assert.AreEqual(expectedResponse, response);
}
Run Code Online (Sandbox Code Playgroud) 我在项目中编写了自定义模型绑定器,它使用ASP.NET MVC 2.此模型绑定器仅绑定2个模型字段:
public class TaskFormBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Name == "Type")
{
var value = bindingContext.ValueProvider.GetValue("Type");
var typeId = value.ConvertTo(typeof(int));
TaskType foundedType;
using (var nhSession = Domain.GetSession())
{
foundedType = nhSession.Get<TaskType>(typeId);
}
if (foundedType != null)
{
SetProperty(controllerContext, bindingContext, propertyDescriptor, foundedType);
}
else
{
AddModelBindError(bindingContext, propertyDescriptor);
}
return;
}
if (propertyDescriptor.Name == "Priority")
{ /* Other field binding ... */
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用标准VS单元测试来测试此模型绑定器?花了几个小时谷歌搜索,找到几个例子( …
我试图重构一段似乎很容易重构的代码,但事实证明很难.有两种方法看起来非常相似,我觉得应该重构: -
public class MyClass
{
private void AddBasicData(Receiver receiver)
{
var aHelper = new AHelper();
var bHelper = new BHelper();
var cHelper = new CHelper();
receiver.ObjA = aHelper.GetBasic();
receiver.ObjB = bHelper.GetBasic();
receiver.ObjC = cHelper.GetBasic();
}
private void AddExistingData(Receiver receiver)
{
var aHelper = new AHelper();
var bHelper = new BHelper();
var cHelper = new CHelper();
receiver.ObjA = aHelper.GetExisting();
receiver.ObjB = bHelper.GetExisting();
receiver.ObjC = cHelper.GetExisting();
}
}
Run Code Online (Sandbox Code Playgroud)
这个类的参考代码在这里......
public class AHelper : Helper<A>
{
}
public class BHelper : Helper<B> …
Run Code Online (Sandbox Code Playgroud) c# ×3
unit-testing ×3
generics ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
javascript ×1
jquery ×1
mocking ×1
modelbinders ×1
moq ×1
nunit ×1
refactoring ×1
testcase ×1