Testing Attribute Behavior
I’m using SharpArch with SharpArch.Contrib’s [Transaction] attribute. The Transaction attribute is added to an application service method and if an exception is thrown during that method then any changes to any domain objects are rolledback. From all appearances this works well.
However, I’m writing NUnit tests to confirm that exceptions are being thrown when appropriate (invalid state, security errors, etc) but I also want to confirm that the Transaction attribute is present and doing its job to rollback the changes. Is there any way I can do this?
I do trust that SharpArch.Contrib’s Transaction attribute is solid code, but so开发者_Python百科me future programmer could accidentally remove the Transaction attribute from a method or disable it during testing which would not be caught by the unit tests. Am I being overly cautious?
Thanks
DanI think you're being a little paranoid, but that's OK :-)
If you trust the SharpArch code, then I wouldn't worry about the exception being thrown back to you correctly. Assume that it works and pick up any problems during integration or functional testing. Testing third party code only has value when you either don't trust it or you're trying to understand it.
On the other hand if you want to test for the presence of an attribute (i.e. validate that the method has the appropriate attribute on it) then you can write a test that uses reflection to inspect the method signature and do some assertions. It's not too hard to do - you just use the methodinfo object to work out the attributes on the method and scan for the TransactionAttribute.
I think testing the transactionality of your code makes perfect sense. I would do it on integration level, when you connect to the real database - then it is easy to generate wrong data, catch the exception and assert that there were no changes done in the database.
I am not sure how you would test it on a unit test level.
精彩评论