起订量和参数属性继承
当我尝试使用 Moq 模拟一个类时,方法属性会继承到模拟类,但不会继承参数属性。
基本上,“let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()”行不会返回属性。运行代码以查看失败的位置。
我怎样才能通过这个测试?
[TestFixture]
public class MyTests {
[Test]
public void shouldFindAndCallMethodWithAttributes() {
var myInterface = new Mock<MyClass>();
myInterface.Setup(x => x.MyMarkedMethod(1));
myInterface.Setup(x => x.MyMarkedMethod(5));
myInterface.Setup(x => x.MyMarkedMethod(9));
var executor = new MarkedMethodExecutor();
executor.FindAndCallMethodWithAttributes(myInterface.Object);
myInterface.VerifyAll();
}
}
public class MarkedMethodExecutor {
public void FindAndCallMethodWithAttributes(object anObject) {
var methods = from m in anObject.GetType().GetMethods()
where m.GetCustomAttributes(typeof (ExecuteMeAttribute), true).SingleOrDefault() != null
select m;
foreach (var method in methods) {
var callInfos = from p in method.GetParameters()
let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()
where a != null
select new {Parameter = p, Attribute = a};
// assume its one argument here for simplicity..
var attribute = callInfos.Single().Attribute;
foreach (var argument in attribute.Arguments) {
method.Invoke(anObject, new[] {argument});
}
}
}
}
public class MyClass {
[ExecuteMe]
public virtual void MyMarkedMethod([Arguments(1, 5, 9)] int arg) {}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ExecuteMeAttribute : Attribute {}
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class ArgumentsAttribute : Attribute {
public readonly object[] Arguments;
public ArgumentsAttribute(params object[] arguments) {
Arguments = arguments;
}
}
When I try to mock a class using Moq the method attribute gets inherited to the mock class, but not the parameter attribute.
Basically the row "let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()" doesnt return the attribute.. Run the code to see where it fails.
How do I make this test pass?
[TestFixture]
public class MyTests {
[Test]
public void shouldFindAndCallMethodWithAttributes() {
var myInterface = new Mock<MyClass>();
myInterface.Setup(x => x.MyMarkedMethod(1));
myInterface.Setup(x => x.MyMarkedMethod(5));
myInterface.Setup(x => x.MyMarkedMethod(9));
var executor = new MarkedMethodExecutor();
executor.FindAndCallMethodWithAttributes(myInterface.Object);
myInterface.VerifyAll();
}
}
public class MarkedMethodExecutor {
public void FindAndCallMethodWithAttributes(object anObject) {
var methods = from m in anObject.GetType().GetMethods()
where m.GetCustomAttributes(typeof (ExecuteMeAttribute), true).SingleOrDefault() != null
select m;
foreach (var method in methods) {
var callInfos = from p in method.GetParameters()
let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()
where a != null
select new {Parameter = p, Attribute = a};
// assume its one argument here for simplicity..
var attribute = callInfos.Single().Attribute;
foreach (var argument in attribute.Arguments) {
method.Invoke(anObject, new[] {argument});
}
}
}
}
public class MyClass {
[ExecuteMe]
public virtual void MyMarkedMethod([Arguments(1, 5, 9)] int arg) {}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ExecuteMeAttribute : Attribute {}
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class ArgumentsAttribute : Attribute {
public readonly object[] Arguments;
public ArgumentsAttribute(params object[] arguments) {
Arguments = arguments;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白你的问题:你想设置属性吗?
您可以使用函数 It.is(match)。也许有帮助:
http://api.moq.me/html/5976987C.htm
I do not understand your problem: do you want to set the attributes?
You could use the function It.is(match). Maybe it helps:
http://api.moq.me/html/5976987C.htm
使用 TypeDescriptor.AddAttributes 方法可以工作吗?我很想知道:
Would futzing with the TypeDescriptor.AddAttributes method work? I'd be curious to know: