I'm trying to do a little Test-First development, and I'm trying to verify that my classes are marked with an attribute:
我正在尝试进行一些Test-First开发,我正在尝试验证我的类是否标有属性:
[SubControllerActionToViewDataAttribute]
public class ScheduleController : Controller
How do I unit test that the class has that attribute assigned to it?
如何对该类具有分配给它的属性进行单元测试?
4 个解决方案
#1
102
check that
检查一下
Attribute.GetCustomAttribute(typeof(ScheduleController),
typeof(SubControllerActionToViewDataAttribute))
isn't null (Assert.IsNotNull
or similar)
不为null(Assert.IsNotNull或类似)
(the reason I use this rather than IsDefined
is that most times I want to validate some properties of the attribute too....)
(我使用它而不是IsDefined的原因是大多数时候我想要验证属性的某些属性....)
#2
60
The same you would normally check for an attribute on a class.
您通常会检查类的属性。
Here's some sample code.
这是一些示例代码。
typeof(ScheduleController)
.IsDefined(typeof(SubControllerActionToViewDataAttribute), false);
I think in many cases testing for the existence of an attribute in a unit test is wrong. As I've not used MVC contrib's sub controller functionality I can't comment whether it is appropriate in this case though.
我认为在许多情况下测试单元测试中是否存在属性是错误的。由于我没有使用MVC contrib的子控制器功能,我无法评论它是否适合这种情况。
#3
8
It is also possible to use generics on this:
也可以在此使用泛型:
var type = typeof(SomeType);
var attribute = type.GetCustomAttribute<SomeAttribute>();
This way you do not need another typeof(...)
, which can make the code cleaner.
这样您就不需要另一种类型(...),这可以使代码更清晰。
#4
5
I know this thread is really old, but if somebody stumble upon on it you may find fluentassertions project very convenient for doing this kind of assertions.
我知道这个线程真的很老,但如果有人偶然发现它你可能会发现fluentassertions项目非常方便做这种断言。
typeof(MyPresentationModel).Should().BeDecoratedWith<SomeAttribute>();
#1
102
check that
检查一下
Attribute.GetCustomAttribute(typeof(ScheduleController),
typeof(SubControllerActionToViewDataAttribute))
isn't null (Assert.IsNotNull
or similar)
不为null(Assert.IsNotNull或类似)
(the reason I use this rather than IsDefined
is that most times I want to validate some properties of the attribute too....)
(我使用它而不是IsDefined的原因是大多数时候我想要验证属性的某些属性....)
#2
60
The same you would normally check for an attribute on a class.
您通常会检查类的属性。
Here's some sample code.
这是一些示例代码。
typeof(ScheduleController)
.IsDefined(typeof(SubControllerActionToViewDataAttribute), false);
I think in many cases testing for the existence of an attribute in a unit test is wrong. As I've not used MVC contrib's sub controller functionality I can't comment whether it is appropriate in this case though.
我认为在许多情况下测试单元测试中是否存在属性是错误的。由于我没有使用MVC contrib的子控制器功能,我无法评论它是否适合这种情况。
#3
8
It is also possible to use generics on this:
也可以在此使用泛型:
var type = typeof(SomeType);
var attribute = type.GetCustomAttribute<SomeAttribute>();
This way you do not need another typeof(...)
, which can make the code cleaner.
这样您就不需要另一种类型(...),这可以使代码更清晰。
#4
5
I know this thread is really old, but if somebody stumble upon on it you may find fluentassertions project very convenient for doing this kind of assertions.
我知道这个线程真的很老,但如果有人偶然发现它你可能会发现fluentassertions项目非常方便做这种断言。
typeof(MyPresentationModel).Should().BeDecoratedWith<SomeAttribute>();