Hi Raphael,
It's documented nowhere yet, but *Assert.HasAttribute* is available in
v3.3.13 <http://ccnet.gallio.org/Distributables/> (and later).
It might be used like this:
[TestFixture]
[TestsOn(typeof(RegisterModel))]
public class RegisterModelTest
{
[Test]
public void Type_should_have_PropertiesMustMatchAttribute()
{
var attribute =
Assert.HasAttribute<PropertiesMustMatchAttribute>(typeof(RegisterModel));
Assert.AreEqual("Password", attribute.OriginalProperty);
Assert.AreEqual("ConfirmPassword", attribute.ConfirmProperty);
Assert.AreEqual("The password and confirmation password do
not match.", attribute.ErrorMessage);
}
[Test]
public void UserName_property_should_have_RequiredAttribute()
{
var member = typeof(RegisterModel).GetProperty("UserName");
Assert.HasAttribute<RequiredAttribute>(member);
}
[Test]
public void UserName_property_should_have_DisplayNameAttribute()
{
var member = typeof(RegisterModel).GetProperty("UserName");
var attribute = Assert.HasAttribute<DisplayNameAttribute>(member);
Assert.AreEqual("User name", attribute.DisplayName);
}
}
Some remarks:
- Instead of targeting the object with *reflection*, you may want to use
the MbUnit Mirror API <http://gallio.org/wiki/doku.php?id=mbunit:mirror>,
which makes easier to get private members.
var member = Mirror.ForType(typeof(RegisterModel))["UserName"];
- In case several instances of the attribute are expected or considered,
it's better to use *Assert.Attributes* instead. It has an optional
*expectedCount
*argument and it returns an array of attributes.
- Non-generic overloads are provided which should be interesting for
data-driven tests.
[Test]
[Column(typeof(RequiredAttribute), typeof(DataTypeAttribute), typeof(
DisplayNameAttribute))]
public void Property_should_have_attributes(Type expectedAttributeType)
{
var member = typeof(RegisterModel).GetProperty("Email");
Assert.HasAttribute(expectedAttributeType, member);
}
- I still consider to implement a few extra overloads which take an
expression to target a property. But it's not implemented yet.
Regards,
Yann.
2010/9/13 Yann Trevin <[email protected]>
> For reference: http://code.google.com/p/mb-unit/issues/detail?id=727
>
> 2010/9/13 Yann Trevin <[email protected]>
>
> You mean by using an expression like in FluentNHibernate?
>> Something like this?
>>
>> class Foo
>> {
>> [Obsolete("Old stuff")]
>> int MyObsoleteProperty
>> {
>> get; set;
>> }
>> }
>>
>> var o = new Foo();
>> var attribute = Assert.HasAttribute<ObsoleteAttribute>(o =>
>> o.MyObsoleteProperty);
>> Assert.AreEqual("Old Stuff", attribute.Message);
>>
>> The assertion could then have the following main signatures:
>>
>> - Attribute Assert.HasAttribute(Type expectedAttributeType,
>> Expression<Func<object, object>> expression)
>> - TAttribute Assert.HasAttribute<TAttribute>(Expression<Func<object,
>> object>> expression)
>>
>> And similarly to Assert.Throws, the assertion returns the instance of the
>> actual attribute, so it's easy to make further assertions on it.
>>
>> What do you think?
>>
>>
>> 2010/9/13 Rafael <[email protected]>
>>
>> Hi Yann,
>>>
>>> I think that .Web extension will be cool!
>>>
>>> Well the attributes can be used for anything not just for the data
>>> validation on MVC,
>>> so I think it's possible to have some general asserts around that
>>> outside the new extension.
>>>
>>> For me I did two things, the first was to check if there is an
>>> attribute on a property,
>>> then I thought in the property that will have attributes like Range
>>> for example,
>>> because don't make sense just check if it exists or not
>>> but it's necessary check the minimum and maximum.
>>>
>>> The first idea is just like you said, Assert.HasAttribute will be
>>> enough
>>> and the second I couldn't figure out a nice name but it's something
>>> that
>>> you'll have basically the types (Model and *Attribute), property name
>>> to check and
>>> the last parameter could be a "Func" that you can check the properties
>>> from the
>>> Attribute.
>>>
>>> Well, that was my idea, if you didn't understand anything I can
>>> clarify, or if didn't like the idea I'll understand too.
>>>
>>> Thanks,
>>> Rafael
>>>
>>> On 13 set, 03:29, Yann Trevin <[email protected]> wrote:
>>> > Hi Rafael,
>>> >
>>> > There is no built-in assertion to do that yet :(
>>> >
>>> > In fact, an extension of MbUnit dedicated to web apps is
>>> > foreseen<http://gallio.org/wiki/doku.php?id=mbunit:ideas>
>>> > (MbUnit.Framework.Web)
>>> > That extension would ideally contain some useful assertions and helper
>>> > features for technologies such as ASP.NET MVC.
>>> > But I don't know precisely what would be worth to add in that
>>> extension...
>>> >
>>> > Otherwise if your requirements are more general, we could easily create
>>> a
>>> > couple of assertions to work with attributes (e.g. Assert.HasAttribute
>>> or
>>> > something)
>>> >
>>> > What sort of assertions would you need?
>>> >
>>> > Yann.
>>> >
>>> > 2010/9/10 Rafael <[email protected]>
>>> >
>>> >
>>> >
>>> > > Hi folks,
>>> >
>>> > > I was trying out the MbUnit Frameworks these days in an ASP.NET MVC
>>> 2
>>> > > application and I wondering how to make an unit test for my models,
>>> > > that uses data annotations for the validation.
>>> >
>>> > > I saw that there's isn't an assertion to check if some property is
>>> > > "Required" for example. And I end up doing by the common way, but I
>>> > > was wondering if these things can be added on the framework (I'm not
>>> > > asking for nothing, just a suggestion) because we need to write some
>>> > > considerable amount of code to check the attribute from a property
>>> > > (even if its possible to create a separated method, like a helper to
>>> > > do that).
>>> >
>>> > > Well, if I the framework provides something to check this kind of
>>> > > thing, please show me, otherwise the suggestion is here!
>>> >
>>> > > Thanks!
>>> >
>>> > > --
>>> > > You received this message because you are subscribed to the Google
>>> Groups
>>> > > "MbUnit.User" group.
>>> > > To post to this group, send email to [email protected].
>>> > > To unsubscribe from this group, send email to
>>> > > [email protected]<mbunituser%[email protected]>
>>> <mbunituser%2bunsubscr...@googlegrou ps.com>
>>> > > .
>>> > > For more options, visit this group at
>>> > >http://groups.google.com/group/mbunituser?hl=en.
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "MbUnit.User" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<mbunituser%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/mbunituser?hl=en.
>>>
>>>
>>
>
--
You received this message because you are subscribed to the Google Groups
"MbUnit.User" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/mbunituser?hl=en.