Re: Fwd: How to test an Exception marked withSerializableAttribute
"Butler, Tony (London)" <[email protected]>
| Newsgroups | gmane.comp.windows.dotnet.nunit.user |
|---|---|
| Message-ID | <289EBA527A6A47418295E92F70ACFB32035021A5@MLLNA225MB.emea.win.ml.com> |
Simon, In general, it would seem that you are testing too much in one [Test], but for fairly simple cases like this, I personally would probably combine them (TDD purists would disagree), because I would probably write the serialisation and deserialisation routines at the same time. One thing I would do though is compare the deserBaseEx values to the baseException values, rather than comparing them to hard coded strings that just happen to be the same in both cases - this makes the intent of the tests clearer - ie, to show that what you put in is what you got out again. cheers Tony. From: [email protected] [mailto:[email protected]] On Behalf Of Simon Garratt Sent: 31 March 2008 11:55 To: [email protected] Subject: [Nunit-users] Fwd: How to test an Exception marked withSerializableAttribute Apologies for the earlier, rather lame "Please help me mail". I'd been up since 3.20am after the darling daughter let go a startled "Daddy!" Needless to say, she was back asleep 30 seconds later and I was still awake come 5.20! Serialization is not an area I'm particularly familiar with. I've come up with a plan and would appreciate feedback. I'm looking to avoid a 'Fragile Test' so please do chip in with any comments. Basically, I'm serializing a new instance of the exception into a MemoryStream, resetting the position and then immediately deserializing it. Then I perform the Asserts on the deserialized object. [ Test] public void BaseException_Serialization() { BaseException baseException = new BaseException("Test message", new ArgumentNullException("NullParam")); BaseException deserBaseEx; BinaryFormatter binaryFormatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, baseException); baseException = null; memoryStream.Position = 0; deserBaseEx = ( BaseException)binaryFormatter.Deserialize(memoryStream); } Assert.That(deserBaseEx.Message, Is.EqualTo("Test message")); Assert.That(deserBaseEx.InnerException, Is.InstanceOfType(typeof(ArgumentNullException))); Assert.That(((ArgumentNullException)deserBaseEx.InnerException).ParamNam e, Is.EqualTo("NullParam")); Assert.That(deserBaseEx.ComputerName, Is.EqualTo(System.Environment.MachineName)); } ---------- Forwarded message ---------- I'm building a base exception class and I want to test it but I can't think how. The bit I'm having trouble with is the fourth required constructor - the serializable one. What's the best way to serialize and deserialize it within a test? -------------------------------------------------------- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -------------------------------------------------------- ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace _______________________________________________ Nunit-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/nunit-users