PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/FluentAssertions.Specs/ObjectAssertionSpecs.cs

#
C# | 836 lines | 484 code | 131 blank | 221 comment | 8 complexity | 46b2159c42ae77d4b8c24b3b159b7771 MD5 | raw file
  1. using System;
  2. using System.Runtime.Serialization;
  3. using System.Xml;
  4. using System.Xml.Schema;
  5. using System.Xml.Serialization;
  6. using FluentAssertions.Assertions;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using FluentAssertions.Common;
  9. namespace FluentAssertions.Specs
  10. {
  11. [TestClass]
  12. public class ObjectAssertionSpecs
  13. {
  14. #region Be / NotBe
  15. [TestMethod]
  16. public void When_two_equal_object_are_expected_to_be_equal_it_should_not_fail()
  17. {
  18. //-------------------------------------------------------------------------------------------------------------------
  19. // Arrange
  20. //-------------------------------------------------------------------------------------------------------------------
  21. var someObject = new ClassWithCustomEqualMethod(1);
  22. var equalObject = new ClassWithCustomEqualMethod(1);
  23. //-------------------------------------------------------------------------------------------------------------------
  24. // Act / Assert
  25. //-------------------------------------------------------------------------------------------------------------------
  26. someObject.Should().Be(equalObject);
  27. }
  28. [TestMethod]
  29. public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_with_a_clear_explanation()
  30. {
  31. //-------------------------------------------------------------------------------------------------------------------
  32. // Arrange
  33. //-------------------------------------------------------------------------------------------------------------------
  34. var someObject = new ClassWithCustomEqualMethod(1);
  35. var nonEqualObject = new ClassWithCustomEqualMethod(2);
  36. //-------------------------------------------------------------------------------------------------------------------
  37. // Act
  38. //-------------------------------------------------------------------------------------------------------------------
  39. Action act = () => someObject.Should().Be(nonEqualObject);
  40. //-------------------------------------------------------------------------------------------------------------------
  41. // Assert
  42. //-------------------------------------------------------------------------------------------------------------------
  43. act.ShouldThrow<AssertFailedException>().WithMessage(
  44. "Expected object to be ClassWithCustomEqualMethod(2), but found ClassWithCustomEqualMethod(1).");
  45. }
  46. [TestMethod]
  47. public void When_both_subject_and_expected_are_null_it_should_succeed()
  48. {
  49. //-------------------------------------------------------------------------------------------------------------------
  50. // Arrange
  51. //-------------------------------------------------------------------------------------------------------------------
  52. object someObject = null;
  53. object expectedObject = null;
  54. //-------------------------------------------------------------------------------------------------------------------
  55. // Act / Assert
  56. //-------------------------------------------------------------------------------------------------------------------
  57. someObject.Should().Be(expectedObject);
  58. }
  59. [TestMethod]
  60. public void When_the_subject_is_null_it_should_fail()
  61. {
  62. //-------------------------------------------------------------------------------------------------------------------
  63. // Arrange
  64. //-------------------------------------------------------------------------------------------------------------------
  65. object someObject = null;
  66. var nonEqualObject = new ClassWithCustomEqualMethod(2);
  67. //-------------------------------------------------------------------------------------------------------------------
  68. // Act
  69. //-------------------------------------------------------------------------------------------------------------------
  70. Action act = () => someObject.Should().Be(nonEqualObject);
  71. //-------------------------------------------------------------------------------------------------------------------
  72. // Assert
  73. //-------------------------------------------------------------------------------------------------------------------
  74. act.ShouldThrow<AssertFailedException>()
  75. .WithMessage("Expected object to be ClassWithCustomEqualMethod(2), but found <null>.");
  76. }
  77. [TestMethod]
  78. public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_and_use_the_reason()
  79. {
  80. //-------------------------------------------------------------------------------------------------------------------
  81. // Arrange
  82. //-------------------------------------------------------------------------------------------------------------------
  83. var someObject = new ClassWithCustomEqualMethod(1);
  84. var nonEqualObject = new ClassWithCustomEqualMethod(2);
  85. //-------------------------------------------------------------------------------------------------------------------
  86. // Act
  87. //-------------------------------------------------------------------------------------------------------------------
  88. Action act = () => someObject.Should().Be(nonEqualObject, "because it should use the {0}", "reason");
  89. //-------------------------------------------------------------------------------------------------------------------
  90. // Assert
  91. //-------------------------------------------------------------------------------------------------------------------
  92. act.ShouldThrow<AssertFailedException>()
  93. .WithMessage(
  94. "Expected object to be ClassWithCustomEqualMethod(2) because it should use the reason, but found ClassWithCustomEqualMethod(1).");
  95. }
  96. [TestMethod]
  97. public void When_non_equal_objects_are_expected_to_be_not_equal_it_should_not_fail()
  98. {
  99. //-------------------------------------------------------------------------------------------------------------------
  100. // Arrange
  101. //-------------------------------------------------------------------------------------------------------------------
  102. var someObject = new ClassWithCustomEqualMethod(1);
  103. var nonEqualObject = new ClassWithCustomEqualMethod(2);
  104. //-------------------------------------------------------------------------------------------------------------------
  105. // Act / Assert
  106. //-------------------------------------------------------------------------------------------------------------------
  107. someObject.Should().NotBe(nonEqualObject);
  108. }
  109. [TestMethod]
  110. public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_with_a_clear_explanation()
  111. {
  112. //-----------------------------------------------------------------------------------------------------------
  113. // Arrange
  114. //-----------------------------------------------------------------------------------------------------------
  115. var someObject = new ClassWithCustomEqualMethod(1);
  116. var equalObject = new ClassWithCustomEqualMethod(1);
  117. //-----------------------------------------------------------------------------------------------------------
  118. // Act
  119. //-----------------------------------------------------------------------------------------------------------
  120. Action act = () =>
  121. someObject.Should().NotBe(equalObject);
  122. //-----------------------------------------------------------------------------------------------------------
  123. // Assert
  124. //-----------------------------------------------------------------------------------------------------------
  125. act.ShouldThrow<AssertFailedException>().WithMessage(
  126. "Did not expect object to be equal to ClassWithCustomEqualMethod(1).");
  127. }
  128. #endregion
  129. #region BeSameAs / NotBeSameAs
  130. [TestMethod]
  131. public void When_the_same_objects_are_expected_to_be_the_same_it_should_not_fail()
  132. {
  133. //-------------------------------------------------------------------------------------------------------------------
  134. // Arrange
  135. //-------------------------------------------------------------------------------------------------------------------
  136. var subject = new ClassWithCustomEqualMethod(1);
  137. var referenceToSubject = subject;
  138. //-------------------------------------------------------------------------------------------------------------------
  139. // Act / Arrange
  140. //-------------------------------------------------------------------------------------------------------------------
  141. subject.Should().BeSameAs(referenceToSubject);
  142. }
  143. [TestMethod]
  144. public void When_two_different_objects_are_expected_to_be_the_same_it_should_fail_with_a_clear_explanation()
  145. {
  146. //-------------------------------------------------------------------------------------------------------------------
  147. // Arrange
  148. //-------------------------------------------------------------------------------------------------------------------
  149. var subject = new
  150. {
  151. Name = "John Doe"
  152. };
  153. var otherObject = new
  154. {
  155. UserName = "JohnDoe"
  156. };
  157. //-------------------------------------------------------------------------------------------------------------------
  158. // Act
  159. //-------------------------------------------------------------------------------------------------------------------
  160. Action act = () => subject.Should().BeSameAs(otherObject, "they are {0} {1}", "the", "same");
  161. //-------------------------------------------------------------------------------------------------------------------
  162. // Assert
  163. //-------------------------------------------------------------------------------------------------------------------
  164. act
  165. .ShouldThrow<AssertFailedException>()
  166. .WithMessage(
  167. "Expected reference to object \r\n{ UserName = JohnDoe } because " +
  168. "they are the same, but found object \r\n{ Name = John Doe }.");
  169. }
  170. [TestMethod]
  171. public void When_two_different_objects_are_expected_not_to_be_the_same_it_should_not_fail()
  172. {
  173. //-------------------------------------------------------------------------------------------------------------------
  174. // Arrange
  175. //-------------------------------------------------------------------------------------------------------------------
  176. var someObject = new ClassWithCustomEqualMethod(1);
  177. var notSameObject = new ClassWithCustomEqualMethod(1);
  178. //-------------------------------------------------------------------------------------------------------------------
  179. // Act / Assert
  180. //-------------------------------------------------------------------------------------------------------------------
  181. someObject.Should().NotBeSameAs(notSameObject);
  182. }
  183. [TestMethod]
  184. public void When_two_equal_object_are_expected_not_to_be_the_same_it_should_fail()
  185. {
  186. //-------------------------------------------------------------------------------------------------------------------
  187. // Arrange
  188. //-------------------------------------------------------------------------------------------------------------------
  189. var someObject = new ClassWithCustomEqualMethod(1);
  190. ClassWithCustomEqualMethod sameObject = someObject;
  191. //-------------------------------------------------------------------------------------------------------------------
  192. // Act
  193. //-------------------------------------------------------------------------------------------------------------------
  194. Action act = () => someObject.Should().NotBeSameAs(sameObject, "they are {0} {1}", "the", "same");
  195. //-------------------------------------------------------------------------------------------------------------------
  196. // Assert
  197. //-------------------------------------------------------------------------------------------------------------------
  198. act.ShouldThrow<AssertFailedException>()
  199. .WithMessage("Did not expect reference to object \r\nClassWithCustomEqualMethod(1) because they are the same.");
  200. }
  201. [TestMethod]
  202. public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_and_use_the_reason()
  203. {
  204. //-----------------------------------------------------------------------------------------------------------
  205. // Arrange
  206. //-----------------------------------------------------------------------------------------------------------
  207. var someObject = new ClassWithCustomEqualMethod(1);
  208. var equalObject = new ClassWithCustomEqualMethod(1);
  209. //-----------------------------------------------------------------------------------------------------------
  210. // Act
  211. //-----------------------------------------------------------------------------------------------------------
  212. Action act = () =>
  213. someObject.Should().NotBe(equalObject, "because we want to test the failure {0}", "message");
  214. //-----------------------------------------------------------------------------------------------------------
  215. // Assert
  216. //-----------------------------------------------------------------------------------------------------------
  217. act.ShouldThrow<AssertFailedException>().WithMessage(
  218. "Did not expect object to be equal to ClassWithCustomEqualMethod(1) " +
  219. "because we want to test the failure message.");
  220. }
  221. #endregion
  222. #region BeNull / BeNotNull
  223. [TestMethod]
  224. public void Should_succeed_when_asserting_null_object_to_be_null()
  225. {
  226. object someObject = null;
  227. someObject.Should().BeNull();
  228. }
  229. [TestMethod]
  230. [ExpectedException(typeof (AssertFailedException))]
  231. public void Should_fail_when_asserting_non_null_object_to_be_null()
  232. {
  233. var someObject = new object();
  234. someObject.Should().BeNull();
  235. }
  236. [TestMethod]
  237. public void When_a_non_null_object_is_expected_to_be_null_it_should_fail()
  238. {
  239. //-----------------------------------------------------------------------------------------------------------
  240. // Arrange
  241. //-----------------------------------------------------------------------------------------------------------
  242. var someObject = new object();
  243. //-----------------------------------------------------------------------------------------------------------
  244. // Act
  245. //-----------------------------------------------------------------------------------------------------------
  246. Action act = () => someObject.Should().BeNull("because we want to test the failure {0}", "message");
  247. //-----------------------------------------------------------------------------------------------------------
  248. // Assert
  249. //-----------------------------------------------------------------------------------------------------------
  250. act
  251. .ShouldThrow<AssertFailedException>()
  252. .Where(e => e.Message.StartsWith("Expected <null> because we want to test the failure message, but found System.Object"));
  253. }
  254. [TestMethod]
  255. public void Should_succeed_when_asserting_non_null_object_not_to_be_null()
  256. {
  257. var someObject = new object();
  258. someObject.Should().NotBeNull();
  259. }
  260. [TestMethod]
  261. [ExpectedException(typeof (AssertFailedException))]
  262. public void Should_fail_when_asserting_null_object_not_to_be_null()
  263. {
  264. object someObject = null;
  265. someObject.Should().NotBeNull();
  266. }
  267. [TestMethod]
  268. public void Should_fail_with_descriptive_message_when_asserting_null_object_not_to_be_null()
  269. {
  270. object someObject = null;
  271. var assertions = someObject.Should();
  272. assertions.Invoking(x => x.NotBeNull("because we want to test the failure {0}", "message"))
  273. .ShouldThrow<AssertFailedException>()
  274. .WithMessage("Expected non-null value because we want to test the failure message, but found <null>.");
  275. }
  276. #endregion
  277. #region BeOfType
  278. [TestMethod]
  279. public void When_object_type_is_exactly_equal_to_the_specified_type_it_should_not_fail()
  280. {
  281. //-------------------------------------------------------------------------------------------------------------------
  282. // Arrange
  283. //-------------------------------------------------------------------------------------------------------------------
  284. var someObject = new Exception();
  285. //-------------------------------------------------------------------------------------------------------------------
  286. // Act
  287. //-------------------------------------------------------------------------------------------------------------------
  288. Action act = () => someObject.Should().BeOfType<Exception>();
  289. //-------------------------------------------------------------------------------------------------------------------
  290. // Assert
  291. //-------------------------------------------------------------------------------------------------------------------
  292. act.ShouldNotThrow();
  293. }
  294. [TestMethod]
  295. public void When_object_type_is_different_than_expected_type_it_should_fail()
  296. {
  297. //-------------------------------------------------------------------------------------------------------------------
  298. // Arrange
  299. //-------------------------------------------------------------------------------------------------------------------
  300. var someObject = new object();
  301. //-------------------------------------------------------------------------------------------------------------------
  302. // Act
  303. //-------------------------------------------------------------------------------------------------------------------
  304. Action act = () => someObject.Should().BeOfType<int>();
  305. //-------------------------------------------------------------------------------------------------------------------
  306. // Assert
  307. //-------------------------------------------------------------------------------------------------------------------
  308. act.ShouldThrow<AssertFailedException>().WithMessage(
  309. "Expected type to be System.Int32, but found System.Object.");
  310. }
  311. [TestMethod]
  312. public void When_object_type_is_different_than_expected_type_it_should_fail_with_descriptive_message()
  313. {
  314. //-------------------------------------------------------------------------------------------------------------------
  315. // Arrange
  316. //-------------------------------------------------------------------------------------------------------------------
  317. var someObject = new object();
  318. //-------------------------------------------------------------------------------------------------------------------
  319. // Act
  320. //-------------------------------------------------------------------------------------------------------------------
  321. Action act = () => someObject.Should().BeOfType<int>("because they are {0} {1}", "of different", "type");
  322. //-------------------------------------------------------------------------------------------------------------------
  323. // Assert
  324. //-------------------------------------------------------------------------------------------------------------------
  325. act.ShouldThrow<AssertFailedException>().WithMessage(
  326. "Expected type to be System.Int32 because they are of different type, but found System.Object.");
  327. }
  328. [TestMethod]
  329. public void When_object_type_is_same_as_expected_type_but_in_different_assembly_it_should_fail_with_assembly_qualified_name()
  330. {
  331. //-------------------------------------------------------------------------------------------------------------------
  332. // Arrange
  333. //-------------------------------------------------------------------------------------------------------------------
  334. var assertionsFromOtherAssembly = new object().Should();
  335. //-------------------------------------------------------------------------------------------------------------------
  336. // Act
  337. //-------------------------------------------------------------------------------------------------------------------
  338. #pragma warning disable 436 // disable the warning on conflicting types, as this is the intention for the spec
  339. Action act = () =>
  340. assertionsFromOtherAssembly.Should().BeOfType<ObjectAssertions>();
  341. #pragma warning restore 436
  342. //-------------------------------------------------------------------------------------------------------------------
  343. // Assert
  344. //-------------------------------------------------------------------------------------------------------------------
  345. const string expectedMessage =
  346. "Expected type to be [FluentAssertions.Assertions.ObjectAssertions, FluentAssertions.*]" +
  347. ", but found [FluentAssertions.Assertions.ObjectAssertions, FluentAssertions*].";
  348. act.ShouldThrow<AssertFailedException>().WithMessage(expectedMessage, ComparisonMode.Wildcard);
  349. }
  350. [TestMethod]
  351. public void When_object_type_is_a_subclass_of_the_expected_type_it_should_fail()
  352. {
  353. //-------------------------------------------------------------------------------------------------------------------
  354. // Arrange
  355. //-------------------------------------------------------------------------------------------------------------------
  356. var someObject = new DummyImplementingClass();
  357. //-------------------------------------------------------------------------------------------------------------------
  358. // Act
  359. //-------------------------------------------------------------------------------------------------------------------
  360. Action act = () => someObject.Should().BeOfType<DummyBaseClass>();
  361. //-------------------------------------------------------------------------------------------------------------------
  362. // Assert
  363. //-------------------------------------------------------------------------------------------------------------------
  364. act.ShouldThrow<AssertFailedException>().WithMessage(
  365. "Expected type to be FluentAssertions.Specs.DummyBaseClass, but found FluentAssertions.Specs.DummyImplementingClass.");
  366. }
  367. #endregion
  368. #region BeAssignableTo
  369. [TestMethod]
  370. public void Should_succeed_when_asserting_object_assignable_to_for_same_type()
  371. {
  372. var someObject = new DummyImplementingClass();
  373. someObject.Should().BeAssignableTo<DummyImplementingClass>();
  374. }
  375. [TestMethod]
  376. public void Should_succeed_when_asserting_object_assignable_to_base_type()
  377. {
  378. var someObject = new DummyImplementingClass();
  379. someObject.Should().BeAssignableTo<DummyBaseClass>();
  380. }
  381. [TestMethod]
  382. public void Should_succeed_when_asserting_object_assignable_to_implemented_interface_type()
  383. {
  384. var someObject = new DummyImplementingClass();
  385. someObject.Should().BeAssignableTo<IDisposable>();
  386. }
  387. [TestMethod]
  388. public void Should_fail_with_descriptive_message_when_asserting_object_assignable_to_not_implemented_type()
  389. {
  390. var someObject = new DummyImplementingClass();
  391. someObject.Invoking(
  392. x => x.Should().BeAssignableTo<DateTime>("because we want to test the failure {0}", "message"))
  393. .ShouldThrow<AssertFailedException>()
  394. .WithMessage(string.Format(
  395. "Expected to be assignable to {1} because we want to test the failure message, but {0} does not implement {1}",
  396. typeof (DummyImplementingClass), typeof (DateTime)));
  397. }
  398. #endregion
  399. #region Miscellaneous
  400. [TestMethod]
  401. public void Should_support_chaining_constraints_with_and()
  402. {
  403. var someObject = new Exception();
  404. someObject.Should()
  405. .BeOfType<Exception>()
  406. .And
  407. .NotBeNull();
  408. }
  409. #endregion
  410. #if !SILVERLIGHT && !WINRT
  411. #region BeBinarySerializable
  412. [TestMethod]
  413. public void When_an_object_is_binary_serializable_it_should_succeed()
  414. {
  415. //-----------------------------------------------------------------------------------------------------------
  416. // Arrange
  417. //-----------------------------------------------------------------------------------------------------------
  418. var subject = new SerializableClass
  419. {
  420. Name = "John"
  421. };
  422. //-----------------------------------------------------------------------------------------------------------
  423. // Act
  424. //-----------------------------------------------------------------------------------------------------------
  425. Action act = () => subject.Should().BeBinarySerializable();
  426. //-----------------------------------------------------------------------------------------------------------
  427. // Assert
  428. //-----------------------------------------------------------------------------------------------------------
  429. act.ShouldNotThrow();
  430. }
  431. [TestMethod]
  432. public void When_an_object_is_not_binary_serializable_it_should_fail()
  433. {
  434. //-----------------------------------------------------------------------------------------------------------
  435. // Arrange
  436. //-----------------------------------------------------------------------------------------------------------
  437. var subject = new UnserializableClass
  438. {
  439. Name = "John"
  440. };
  441. //-----------------------------------------------------------------------------------------------------------
  442. // Act
  443. //-----------------------------------------------------------------------------------------------------------
  444. Action act = () => subject.Should().BeBinarySerializable("we need to store it on {0}", "disk");
  445. //-----------------------------------------------------------------------------------------------------------
  446. // Assert
  447. //-----------------------------------------------------------------------------------------------------------
  448. act
  449. .ShouldThrow<AssertFailedException>()
  450. .Where(ex =>
  451. ex.Message.Contains("to be serializable because we need to store it on disk, but serialization failed with:") &&
  452. ex.Message.Contains("marked as serializable"));
  453. }
  454. [TestMethod]
  455. public void When_an_object_is_binary_serializable_but_not_deserializable_it_should_fail()
  456. {
  457. //-----------------------------------------------------------------------------------------------------------
  458. // Arrange
  459. //-----------------------------------------------------------------------------------------------------------
  460. var subject = new BinarySerializableClassMissingDeserializationConstructor
  461. {
  462. Name = "John",
  463. BirthDay = 20.September(1973)
  464. };
  465. //-----------------------------------------------------------------------------------------------------------
  466. // Act
  467. //-----------------------------------------------------------------------------------------------------------
  468. Action act = () => subject.Should().BeBinarySerializable();
  469. //-----------------------------------------------------------------------------------------------------------
  470. // Assert
  471. //-----------------------------------------------------------------------------------------------------------
  472. act
  473. .ShouldThrow<AssertFailedException>()
  474. .Where(ex =>
  475. ex.Message.Contains("to be serializable, but serialization failed with:") &&
  476. ex.Message.Contains("constructor to deserialize"));
  477. }
  478. [TestMethod]
  479. public void When_an_object_is_binary_serializable_but_doesnt_restore_all_properties_it_should_fail()
  480. {
  481. //-----------------------------------------------------------------------------------------------------------
  482. // Arrange
  483. //-----------------------------------------------------------------------------------------------------------
  484. var subject = new BinarySerializableClassNotRestoringAllProperties
  485. {
  486. Name = "John",
  487. BirthDay = 20.September(1973)
  488. };
  489. //-----------------------------------------------------------------------------------------------------------
  490. // Act
  491. //-----------------------------------------------------------------------------------------------------------
  492. Action act = () => subject.Should().BeBinarySerializable();
  493. //-----------------------------------------------------------------------------------------------------------
  494. // Assert
  495. //-----------------------------------------------------------------------------------------------------------
  496. act
  497. .ShouldThrow<AssertFailedException>()
  498. .Where(ex =>
  499. ex.Message.Contains("to be serializable, but serialization failed with:") &&
  500. ex.Message.Contains("property Name to be"));
  501. }
  502. internal class UnserializableClass
  503. {
  504. public string Name { get; set; }
  505. }
  506. [Serializable]
  507. public class SerializableClass
  508. {
  509. public string Name { get; set; }
  510. }
  511. [Serializable]
  512. internal class BinarySerializableClassMissingDeserializationConstructor : ISerializable
  513. {
  514. public string Name { get; set; }
  515. public DateTime BirthDay { get; set; }
  516. public void GetObjectData(SerializationInfo info, StreamingContext context)
  517. {
  518. }
  519. }
  520. [Serializable]
  521. internal class BinarySerializableClassNotRestoringAllProperties : ISerializable
  522. {
  523. public string Name { get; set; }
  524. public DateTime BirthDay { get; set; }
  525. public BinarySerializableClassNotRestoringAllProperties()
  526. {
  527. }
  528. public BinarySerializableClassNotRestoringAllProperties(SerializationInfo info, StreamingContext context)
  529. {
  530. BirthDay = info.GetDateTime("BirthDay");
  531. }
  532. public void GetObjectData(SerializationInfo info, StreamingContext context)
  533. {
  534. info.AddValue("BirthDay", BirthDay);
  535. }
  536. }
  537. #endregion
  538. #endif
  539. #region BeXmlSerializable
  540. [TestMethod]
  541. public void When_an_object_is_xml_serializable_it_should_succeed()
  542. {
  543. //-----------------------------------------------------------------------------------------------------------
  544. // Arrange
  545. //-----------------------------------------------------------------------------------------------------------
  546. var subject = new XmlSerializableClass
  547. {
  548. Name = "John"
  549. };
  550. //-----------------------------------------------------------------------------------------------------------
  551. // Act
  552. //-----------------------------------------------------------------------------------------------------------
  553. Action act = () => subject.Should().BeXmlSerializable();
  554. //-----------------------------------------------------------------------------------------------------------
  555. // Assert
  556. //-----------------------------------------------------------------------------------------------------------
  557. act.ShouldNotThrow();
  558. }
  559. [TestMethod]
  560. public void When_an_object_is_not_xml_serializable_it_should_fail()
  561. {
  562. //-----------------------------------------------------------------------------------------------------------
  563. // Arrange
  564. //-----------------------------------------------------------------------------------------------------------
  565. var subject = new NonPublicClass
  566. {
  567. Name = "John"
  568. };
  569. //-----------------------------------------------------------------------------------------------------------
  570. // Act
  571. //-----------------------------------------------------------------------------------------------------------
  572. Action act = () => subject.Should().BeXmlSerializable("we need to store it on {0}", "disk");
  573. //-----------------------------------------------------------------------------------------------------------
  574. // Assert
  575. //-----------------------------------------------------------------------------------------------------------
  576. act
  577. .ShouldThrow<AssertFailedException>()
  578. .Where(ex =>
  579. ex.Message.Contains("to be serializable because we need to store it on disk, but serialization failed with:") &&
  580. ex.Message.Contains("Only public types can be processed"));
  581. }
  582. [TestMethod]
  583. public void When_an_object_is_xml_serializable_but_doesnt_restore_all_properties_it_should_fail()
  584. {
  585. //-----------------------------------------------------------------------------------------------------------
  586. // Arrange
  587. //-----------------------------------------------------------------------------------------------------------
  588. var subject = new XmlSerializableClassNotRestoringAllProperties
  589. {
  590. Name = "John",
  591. BirthDay = 20.September(1973)
  592. };
  593. //-----------------------------------------------------------------------------------------------------------
  594. // Act
  595. //-----------------------------------------------------------------------------------------------------------
  596. Action act = () => subject.Should().BeXmlSerializable();
  597. //-----------------------------------------------------------------------------------------------------------
  598. // Assert
  599. //-----------------------------------------------------------------------------------------------------------
  600. act
  601. .ShouldThrow<AssertFailedException>()
  602. .Where(ex =>
  603. ex.Message.Contains("to be serializable, but serialization failed with:") &&
  604. ex.Message.Contains("property Name to be"));
  605. }
  606. internal class NonPublicClass
  607. {
  608. public string Name { get; set; }
  609. }
  610. public class XmlSerializableClass
  611. {
  612. public string Name { get; set; }
  613. }
  614. public class XmlSerializableClassNotRestoringAllProperties : IXmlSerializable
  615. {
  616. public string Name { get; set; }
  617. public DateTime BirthDay { get; set; }
  618. public XmlSchema GetSchema()
  619. {
  620. return null;
  621. }
  622. public void ReadXml(XmlReader reader)
  623. {
  624. BirthDay = DateTime.Parse(reader.ReadElementContentAsString());
  625. }
  626. public void WriteXml(XmlWriter writer)
  627. {
  628. writer.WriteString(BirthDay.ToString());
  629. }
  630. }
  631. #endregion
  632. }
  633. internal class ClassWithCustomEqualMethod
  634. {
  635. /// <summary>
  636. /// Initializes a new instance of the <see cref = "T:System.Object" /> class.
  637. /// </summary>
  638. public ClassWithCustomEqualMethod(int key)
  639. {
  640. Key = key;
  641. }
  642. private int Key { get; set; }
  643. private bool Equals(ClassWithCustomEqualMethod other)
  644. {
  645. if (ReferenceEquals(null, other))
  646. {
  647. return false;
  648. }
  649. if (ReferenceEquals(this, other))
  650. {
  651. return true;
  652. }
  653. return other.Key == Key;
  654. }
  655. public override bool Equals(object obj)
  656. {
  657. if (ReferenceEquals(null, obj))
  658. {
  659. return false;
  660. }
  661. if (ReferenceEquals(this, obj))
  662. {
  663. return true;
  664. }
  665. if (obj.GetType() != typeof (ClassWithCustomEqualMethod))
  666. {
  667. return false;
  668. }
  669. return Equals((ClassWithCustomEqualMethod) obj);
  670. }
  671. public override int GetHashCode()
  672. {
  673. return Key;
  674. }
  675. public static bool operator ==(ClassWithCustomEqualMethod left, ClassWithCustomEqualMethod right)
  676. {
  677. return Equals(left, right);
  678. }
  679. public static bool operator !=(ClassWithCustomEqualMethod left, ClassWithCustomEqualMethod right)
  680. {
  681. return !Equals(left, right);
  682. }
  683. /// <summary>
  684. /// Returns a <see cref = "T:System.String" /> that represents the current <see cref = "T:System.Object" />.
  685. /// </summary>
  686. /// <returns>
  687. /// A <see cref = "T:System.String" /> that represents the current <see cref = "T:System.Object" />.
  688. /// </returns>
  689. /// <filterpriority>2</filterpriority>
  690. public override string ToString()
  691. {
  692. return string.Format("ClassWithCustomEqualMethod({0})", Key);
  693. }
  694. }
  695. internal class DummyBaseClass
  696. {
  697. }
  698. internal class DummyImplementingClass : DummyBaseClass, IDisposable
  699. {
  700. public void Dispose()
  701. {
  702. // Ignore
  703. }
  704. }
  705. }