Sunday, 15 June 2014

c# - How do I test an exception message containing localized dates in nunit -



c# - How do I test an exception message containing localized dates in nunit -

i've come across interesting problem 1 of our unit tests. it's case we're trying ensure when particular exceptional case encountered, throw , provide useful message handler - thought handler can display (or log) message directly. problem when message contains date - want date expressed using users locale , overrides may have specified on local system.

the problem arises because have 2 different ci infrastructures (don't inquire - 1 development, 1 release) various reasons, have 2 (slightly) different date settings - 1 passes test below, while other uses (customized) date format similar "14jan2014".

[test] [expectedexception(typeof(invalidoperationexception), expectedmessage="invalid date: 14/01/2014")] public void exampletest() { var date = new datetime(2014, 01, 14); throw new invalidoperationexception("invalid date: " + date.tostring()); }

i aware of setculture , culture attributes don't believe these work due customized aspect of date format on machine. possible somehow ignore customised settings , @ to the lowest degree utilize locale's defaults purposes of test?

you can explicitly grab exception mentioned @dave mackersie in reply or can utilize expectedexception attribute exception handler

[expectedexception(handler = "handleexception")]

your test

[test] [expectedexception(handler = "handleexception")] public void exampletest() { var date = new datetime(2014, 01, 14); throw new invalidoperationexception("invalid date: " + date.tostring()); } public void handleexception(exception ex) { if (!(ex invalidoperationexception)) assert.fail("unexpected type of exception thrown method"); // compare expected exception message ex.message // assert.fail if not match }

c# unit-testing nunit

No comments:

Post a Comment