/
ObjectAssertionSpecs.cs
1546 lines (1311 loc) · 81.9 KB
/
ObjectAssertionSpecs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using FluentAssertions.Extensions;
using FluentAssertions.Primitives;
using Xunit;
using Xunit.Sdk;
namespace FluentAssertions.Specs
{
public class ObjectAssertionSpecs
{
#region Be / NotBe
[Fact]
public void When_two_equal_object_are_expected_to_be_equal_it_should_not_fail()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new ClassWithCustomEqualMethod(1);
var equalObject = new ClassWithCustomEqualMethod(1);
//-------------------------------------------------------------------------------------------------------------------
// Act / Assert
//-------------------------------------------------------------------------------------------------------------------
someObject.Should().Be(equalObject);
}
[Fact]
public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_with_a_clear_explanation()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new ClassWithCustomEqualMethod(1);
var nonEqualObject = new ClassWithCustomEqualMethod(2);
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().Be(nonEqualObject);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
#if NETCOREAPP1_1
"Expected object to be ClassWithCustomEqualMethod(2), but found ClassWithCustomEqualMethod(1).");
#else
"Expected someObject to be ClassWithCustomEqualMethod(2), but found ClassWithCustomEqualMethod(1).");
#endif
}
[Fact]
public void When_both_subject_and_expected_are_null_it_should_succeed()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
object someObject = null;
object expectedObject = null;
//-------------------------------------------------------------------------------------------------------------------
// Act / Assert
//-------------------------------------------------------------------------------------------------------------------
someObject.Should().Be(expectedObject);
}
[Fact]
public void When_the_subject_is_null_it_should_fail()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
object someObject = null;
var nonEqualObject = new ClassWithCustomEqualMethod(2);
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().Be(nonEqualObject);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
#if NETCOREAPP1_1
.WithMessage("Expected object to be ClassWithCustomEqualMethod(2), but found <null>.");
#else
.WithMessage("Expected someObject to be ClassWithCustomEqualMethod(2), but found <null>.");
#endif
}
[Fact]
public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_and_use_the_reason()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new ClassWithCustomEqualMethod(1);
var nonEqualObject = new ClassWithCustomEqualMethod(2);
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().Be(nonEqualObject, "because it should use the {0}", "reason");
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage(
#if NETCOREAPP1_1
"Expected object to be ClassWithCustomEqualMethod(2) because it should use the reason, but found ClassWithCustomEqualMethod(1).");
#else
"Expected someObject to be ClassWithCustomEqualMethod(2) because it should use the reason, but found ClassWithCustomEqualMethod(1).");
#endif
}
[Fact]
public void When_non_equal_objects_are_expected_to_be_not_equal_it_should_not_fail()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new ClassWithCustomEqualMethod(1);
var nonEqualObject = new ClassWithCustomEqualMethod(2);
//-------------------------------------------------------------------------------------------------------------------
// Act / Assert
//-------------------------------------------------------------------------------------------------------------------
someObject.Should().NotBe(nonEqualObject);
}
[Fact]
public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_with_a_clear_explanation()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new ClassWithCustomEqualMethod(1);
var equalObject = new ClassWithCustomEqualMethod(1);
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () =>
someObject.Should().NotBe(equalObject);
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
#if NETCOREAPP1_1
"Did not expect object to be equal to ClassWithCustomEqualMethod(1).");
#else
"Did not expect someObject to be equal to ClassWithCustomEqualMethod(1).");
#endif
}
[Fact]
public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_and_use_the_reason()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new ClassWithCustomEqualMethod(1);
var equalObject = new ClassWithCustomEqualMethod(1);
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () =>
someObject.Should().NotBe(equalObject, "because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
#if NETCOREAPP1_1
"Did not expect object to be equal to ClassWithCustomEqualMethod(1) " +
"because we want to test the failure message.");
#else
"Did not expect someObject to be equal to ClassWithCustomEqualMethod(1) " +
"because we want to test the failure message.");
#endif
}
#endregion
#region BeNull / BeNotNull
[Fact]
public void Should_succeed_when_asserting_null_object_to_be_null()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
object someObject = null;
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeNull();
}
[Fact]
public void Should_fail_when_asserting_non_null_object_to_be_null()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new object();
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeNull();
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>();
}
[Fact]
public void When_a_non_null_object_is_expected_to_be_null_it_should_fail()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new object();
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeNull("because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act
.Should().Throw<XunitException>()
.Where(e => e.Message.StartsWith(
#if NETCOREAPP1_1
"Expected object to be <null> because we want to test the failure message, but found System.Object"));
#else
"Expected someObject to be <null> because we want to test the failure message, but found System.Object"));
#endif
}
[Fact]
public void Should_succeed_when_asserting_non_null_object_not_to_be_null()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new object();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().NotBeNull();
}
[Fact]
public void Should_fail_when_asserting_null_object_not_to_be_null()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
object someObject = null;
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().NotBeNull();
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>();
}
[Fact]
public void Should_fail_with_descriptive_message_when_asserting_null_object_not_to_be_null()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
object someObject = null;
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().NotBeNull("because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
#if NETCOREAPP1_1
"Expected object not to be <null> because we want to test the failure message.");
#else
"Expected someObject not to be <null> because we want to test the failure message.");
#endif
}
#endregion
#region BeOfType / NotBeOfType
[Fact]
public void When_object_type_is_exactly_equal_to_the_specified_type_it_should_not_fail()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new Exception();
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeOfType<Exception>();
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().NotThrow();
}
[Fact]
public void When_object_type_is_value_type_and_matches_received_type_should_not_fail_and_assert_correctly()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
int valueTypeObject = 42;
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => valueTypeObject.Should().BeOfType(typeof(int));
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().NotThrow();
}
[Fact]
public void When_object_type_is_value_type_and_doesnt_match_received_type_as_expected_should_not_fail_and_assert_correctly()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
int valueTypeObject = 42;
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => valueTypeObject.Should().NotBeOfType(typeof(double));
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().NotThrow();
}
[Fact]
public void When_object_type_is_value_type_and_matches_received_type_not_as_expected_should_fail()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
int valueTypeObject = 42;
var expectedType = typeof(int);
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => valueTypeObject.Should().NotBeOfType(expectedType);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage($"Expected type not to be [{expectedType.AssemblyQualifiedName}], but it is.");
}
[Fact]
public void When_object_type_is_value_type_and_doesnt_match_received_type_should_fail()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
int valueTypeObject = 42;
var doubleType = typeof(double);
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => valueTypeObject.Should().BeOfType(doubleType);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage($"Expected type to be {doubleType}, but found {valueTypeObject.GetType()}.");
}
[Fact]
public void When_object_is_of_the_expected_type_it_should_cast_the_returned_object_for_chaining()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new Exception("Actual Message");
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeOfType<Exception>().Which.Message.Should().Be("Other Message");
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("*Expected*Other*Actual*");
}
[Fact]
public void When_object_type_is_different_than_expected_type_it_should_fail_with_descriptive_message()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new object();
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeOfType<int>("because they are {0} {1}", "of different", "type");
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
"Expected type to be System.Int32 because they are of different type, but found System.Object.");
}
[Fact]
public void When_asserting_the_type_of_a_null_object_it_should_throw()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
object someObject = null;
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeOfType<int>();
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
#if NETCOREAPP1_1
.WithMessage("Expected type to be System.Int32, but found <null>.");
#else
.WithMessage("Expected someObject to be System.Int32, but found <null>.");
#endif
}
[Fact]
public void Then_object_type_is_same_as_expected_type_but_in_different_assembly_it_should_fail_with_assembly_qualified_name
()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var assertionsFromOtherAssembly = new object().Should();
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
#pragma warning disable 436 // disable the warning on conflicting types, as this is the intention for the spec
Action act = () =>
assertionsFromOtherAssembly.Should().BeOfType<ObjectAssertions>();
#pragma warning restore 436
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
const string expectedMessage =
"Expected type to be [FluentAssertions.Primitives.ObjectAssertions, FluentAssertions.*]" +
", but found [FluentAssertions.Primitives.ObjectAssertions, FluentAssertions*].";
act.Should().Throw<XunitException>().WithMessage(expectedMessage);
}
[Fact]
public void When_object_type_is_a_subclass_of_the_expected_type_it_should_fail()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeOfType<DummyBaseClass>();
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
"Expected type to be FluentAssertions.Specs.DummyBaseClass, but found FluentAssertions.Specs.DummyImplementingClass.");
}
#endregion
#region BeAssignableTo
[Fact]
public void When_its_own_type_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeAssignableTo<DummyImplementingClass>();
}
[Fact]
public void When_its_base_type_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeAssignableTo<DummyBaseClass>();
}
[Fact]
public void When_an_implemented_interface_type_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeAssignableTo<IDisposable>();
}
[Fact]
public void When_an_unrelated_type_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().BeAssignableTo<DateTime>("because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*assignable to {typeof(DateTime)}*failure message*{typeof(DummyImplementingClass)} is not*");
}
[Fact]
public void When_to_the_expected_type_it_should_cast_the_returned_object_for_chaining()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new Exception("Actual Message");
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().BeAssignableTo<Exception>().Which.Message.Should().Be("Other Message");
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("*Expected*Other*Actual*");
}
[Fact]
public void When_its_own_type_instance_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeAssignableTo(typeof(DummyImplementingClass));
}
[Fact]
public void When_its_base_type_instance_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeAssignableTo(typeof(DummyBaseClass));
}
[Fact]
public void When_an_implemented_interface_type_instance_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeAssignableTo(typeof(IDisposable));
}
[Fact]
public void When_an_implemented_open_generic_interface_type_instance_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new System.Collections.Generic.List<string>();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().BeAssignableTo(typeof(System.Collections.Generic.IList<>));
}
[Fact]
public void When_an_unrelated_type_instance_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().BeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*assignable to {typeof(DateTime)}*failure message*{typeof(DummyImplementingClass)} is not*");
}
[Fact]
public void When_unrelated_to_open_generic_type_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().BeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*assignable to {typeof(System.Collections.Generic.IList<>)}*failure message*{typeof(DummyImplementingClass)} is not*");
}
#endregion
#region NotBeAssignableTo
[Fact]
public void When_its_own_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().NotBeAssignableTo<DummyImplementingClass>("because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*not be assignable to {typeof(DummyImplementingClass)}*failure message*{typeof(DummyImplementingClass)} is*");
}
[Fact]
public void When_its_base_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().NotBeAssignableTo<DummyBaseClass>("because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*not be assignable to {typeof(DummyBaseClass)}*failure message*{typeof(DummyImplementingClass)} is*");
}
[Fact]
public void When_an_implemented_interface_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().NotBeAssignableTo<IDisposable>("because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*not be assignable to {typeof(IDisposable)}*failure message*{typeof(DummyImplementingClass)} is*");
}
[Fact]
public void When_an_unrelated_type_and_asserting_not_assignable_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().NotBeAssignableTo<DateTime>();
}
[Fact]
public void When_not_to_the_unexpected_type_and_asserting_not_assignable_it_should_not_cast_the_returned_object_for_chaining()
{
//-------------------------------------------------------------------------------------------------------------------
// Arrange
//-------------------------------------------------------------------------------------------------------------------
var someObject = new Exception("Actual Message");
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().NotBeAssignableTo<DummyImplementingClass>()
.And.Subject.Should().BeOfType<Exception>()
.Which.Message.Should().Be("Other Message");
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("*Expected*Other*Actual*");
}
[Fact]
public void When_its_own_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().NotBeAssignableTo(typeof(DummyImplementingClass), "because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*not be assignable to {typeof(DummyImplementingClass)}*failure message*{typeof(DummyImplementingClass)} is*");
}
[Fact]
public void When_its_base_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().NotBeAssignableTo(typeof(DummyBaseClass), "because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*not be assignable to {typeof(DummyBaseClass)}*failure message*{typeof(DummyImplementingClass)} is*");
}
[Fact]
public void When_an_implemented_interface_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
Action act = () => someObject.Should().NotBeAssignableTo(typeof(IDisposable), "because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*not be assignable to {typeof(IDisposable)}*failure message*{typeof(DummyImplementingClass)} is*");
}
[Fact]
public void When_an_implemented_open_generic_interface_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new System.Collections.Generic.List<string>();
Action act = () => someObject.Should().NotBeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message");
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage($"*not be assignable to {typeof(System.Collections.Generic.IList<>)}*failure message*{typeof(System.Collections.Generic.List<string>)} is*");
}
[Fact]
public void When_an_unrelated_type_instance_and_asserting_not_assignable_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().NotBeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message");
}
[Fact]
public void When_unrelated_to_open_generic_type_and_asserting_not_assignable_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var someObject = new DummyImplementingClass();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
someObject.Should().NotBeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message");
}
#endregion
#region HaveFlag / NotHaveFlag
[Fact]
public void When_enum_has_the_expected_flag_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
TestEnum someObject = TestEnum.One | TestEnum.Two;
//-------------------------------------------------------------------------------------------------------------------
// Act / Assert
//-------------------------------------------------------------------------------------------------------------------
someObject.Should().HaveFlag(TestEnum.Two);
}
[Fact]
public void When_object_is_not_enum_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
object someObject = new object();
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().HaveFlag(TestEnum.Three);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("Expected*type*TestEnum*Object*");
}
[Fact]
public void When_object_is_not_enum_and_null_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
object someObject = null;
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().HaveFlag(TestEnum.Three);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("Expected*type*but found <null>*");
}
[Fact]
public void When_enum_does_not_have_specified_flag_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
TestEnum someObject = TestEnum.One | TestEnum.Two;
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().HaveFlag(TestEnum.Three);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("The enum was expected to have flag Three but found One, Two.");
}
[Fact]
public void When_enum_is_not_of_the_same_type_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
TestEnum someObject = TestEnum.One | TestEnum.Two;
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().HaveFlag(OtherEnum.First);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("Expected*type*OtherEnum*TestEnum*");
}
[Fact]
public void When_enum_does_not_have_the_unexpected_flag_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
TestEnum someObject = TestEnum.One | TestEnum.Two;
//-------------------------------------------------------------------------------------------------------------------
// Act / Assert
//-------------------------------------------------------------------------------------------------------------------
someObject.Should().NotHaveFlag(TestEnum.Three);
}
[Fact]
public void When_enum_does_have_specified_flag_it_should_fail_with_a_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
TestEnum someObject = TestEnum.One | TestEnum.Two;
//-------------------------------------------------------------------------------------------------------------------
// Act
//-------------------------------------------------------------------------------------------------------------------
Action act = () => someObject.Should().NotHaveFlag(TestEnum.Two);
//-------------------------------------------------------------------------------------------------------------------
// Assert
//-------------------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("Did not expect the enum to have flag Two.");
}