c# - Should I Write Tests for Argument Exceptions? -
Text after "
When applying TDD, you test that exception to the argument (ArgumentException, ArgumentNullException, InvalidOperation, etc.) Can verify or just create "known" ones, for example, CustomerDelinquentException?
What's the gethashcode override about equals? How would I test gethashcode?
Thanks
I always test for any exceptions I throw in my method , Which includes As far as You can try to test two non-equivalent objects change different hash codes, but you have to make sure that you use them The values that go are just a collision It depends largely on the algorithm in the code Check for equals () with two field equal to For even more fun, I like the unit test Then you can install a unit test to inject a specific date time. logic exception ,
logic expression, , etc. I'm just the best to check them and they're easy to write. If ever someone those guards, the test will break and you want to know.
[TestMethod] [ExpectedException (typeof (ArgumentNullException)]] Public Zero Delete ToSetOnNullThrows () {List & lt; String & gt; List = null; Var target = list.ToHashSet (); }
GetHashCode () and
is equal to () I test these as well < Em> if you have them override / em> An easy test for
GetHashCode () is to create two equal objects (the same value used in the hash code) and it proves that the hash codes with both the objects are identical.
[TestMethod] Public Zero GetHashCodeSameKeysAreSameTest () {var key = new CompositeKey & lt; String, int & gt; ("A", 13); Var others = new composite & lt; String, At & G; ("A", 13); Confirm. Eraquall (Keygatehashcod), OtherKGhashCode ()); }
GetHashCode ()
[TestMethod] Public Zero GetHashCodeDifferentKeysAreMostLikelyDifferentTest () {var Key = New CompositeKey & Lt; String, integer & gt; ("A", 13); Var others = new composite & lt; String, int & gt; ("A", 14); Confirm. No, properties (key. GetHashCode (), other key. GetHashCode ()); }
true with
equal to
and
False two non-equal items
[TestMethod] Public Zero EqualsTest () {var key = New CompositeKey & Lt; String, integer & gt; ('A', 13); Var others = new composite & lt; String, At & G; ("A", 13); Assert.IsTrue (key.Equals (otherKey)); } [TestMethod] Public Zero NotEqualsTest () {var key = new CompositeKey & lt; String, int & gt; ("A", 13); Var others = new composite & lt; String, int a (g, "a", 15); Assert.IsFalse (key.Equals (otherKey)); }
DateTime dependent stuff. It is a bit difficult, but if the behavior of any method depends on the
date time , then I still want them to do the unit testing. You can create a
datetime generator representative, which is the default for returning to
DateTime.Now but such that you have a specific
DateTime Can set the generator for. Ever since I am in the financial industry and a lot of logic depends on the pre-market, post-market time, since then there is a lot of coverage in the field of my work ...
< Code> Public Class SomeClassThatDependsOnCurrentTime {Internal Function & lt; DateTime & gt; Now get the generator { Set; } Public SomeClassThatDependsOnCurrentTime () {// default to dateTime.Now to return to default in the constructor = () = & gt; DateTime.Now; } Public bool IsAfterMarketClose () {// Make a generator call instead of date of call.Now directly ... back to IZ (generator). TimeOfWay & gt; New TimeSpain (16, 0, 0); }}
Comments
Post a Comment