@@ -49,6 +49,10 @@ class ControllerResultTestShould
4949 ReturnType < FileResult > ( t => t . ShouldRenderAnyFile ( ) ) ,
5050 ReturnType < HttpStatusCodeResult > ( t => t . ShouldGiveHttpStatus ( ) ) ,
5151 ReturnType < JsonResult > ( t => t . ShouldReturnJson ( ) ) ,
52+ ReturnType < ContentResult > ( t => t . ShouldReturnContent ( ) ) ,
53+ ReturnType < ContentResult > ( t => t . ShouldReturnContent ( "" ) ) ,
54+ ReturnType < ContentResult > ( t => t . ShouldReturnContent ( "" , "" ) ) ,
55+ ReturnType < ContentResult > ( t => t . ShouldReturnContent ( "" , "" , Encoding . UTF8 ) )
5256 } ;
5357 // Different ways that action redirects can be asserted along with the expected method name and the correct controller action call for that assertion
5458 private static readonly List < RedirectToActionTestMetadata > ActionRedirects = new List < RedirectToActionTestMetadata >
@@ -770,5 +774,84 @@ public void Allow_the_object_that_is_returned_to_be_checked()
770774 _controller . WithCallTo ( c => c . Json ( ) ) . ShouldReturnJson ( d => Assert . That ( d , Is . EqualTo ( ControllerResultTestController . JsonValue ) ) ) ;
771775 }
772776 #endregion
777+
778+ #region Content tests
779+
780+ [ Test ]
781+ public void Check_for_content_result ( )
782+ {
783+ _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( ) ;
784+ }
785+
786+ [ Test ]
787+ public void Check_for_content_result_and_check_content ( )
788+ {
789+ _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( ControllerResultTestController . TextualContent ) ;
790+ }
791+
792+ [ Test ]
793+ public void Check_for_content_result_and_check_invalid_content ( )
794+ {
795+ const string content = "dummy contents" ;
796+
797+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) => _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( content ) ) ;
798+
799+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected content to be \" {0}\" , but instead was \" {1}\" ." , content , ControllerResultTestController . TextualContent ) ) ) ;
800+ }
801+
802+ [ Test ]
803+ public void Check_for_content_result_and_check_content_and_check_content_type ( )
804+ {
805+ _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( ControllerResultTestController . TextualContent , ControllerResultTestController . ContentType ) ;
806+ }
807+
808+ [ Test ]
809+ public void Check_for_content_result_and_check_content_and_check_invalid_content_type ( )
810+ {
811+ const string contentType = "application/dummy" ;
812+
813+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) => _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( ControllerResultTestController . TextualContent , contentType ) ) ;
814+
815+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected content type to be \" {0}\" , but instead was \" {1}\" ." , contentType , ControllerResultTestController . ContentType ) ) ) ;
816+ }
817+
818+ [ Test ]
819+ public void Check_for_content_result_and_check_content_and_check_content_type_and_check_content_encoding ( )
820+ {
821+ _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( ControllerResultTestController . TextualContent , ControllerResultTestController . ContentType , ControllerResultTestController . TextualContentEncoding ) ;
822+ }
823+
824+ [ Test ]
825+ public void Check_for_content_result_and_check_content_and_check_content_type_and_check_invalid_content_encoding ( )
826+ {
827+ var encoding = Encoding . Unicode ;
828+
829+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) => _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( ControllerResultTestController . TextualContent , ControllerResultTestController . ContentType , encoding ) ) ;
830+
831+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected encoding to be equal to {0}, but instead was {1}." , encoding . EncodingName , ControllerResultTestController . TextualContentEncoding . EncodingName ) ) ) ;
832+ }
833+
834+ [ Test ]
835+ public void Check_for_content_result_and_check_invalid_content_and_check_invalid_content_type_and_check_invalid_encoding ( )
836+ {
837+ const string contentType = "application/dummy" ;
838+ const string content = "dumb" ;
839+ Encoding encoding = Encoding . Unicode ;
840+
841+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) => _controller . WithCallTo ( c => c . Content ( ) ) . ShouldReturnContent ( content , contentType , encoding ) ) ;
842+
843+ // Assert that the content type validation occurs before that of the actual content.
844+ Assert . That ( exception . Message . Contains ( "content type" ) ) ;
845+ }
846+
847+ [ Test ]
848+ public void Emit_readable_error_message_when_the_actual_content_encoding_has_not_been_specified ( )
849+ {
850+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) => _controller . WithCallTo ( c => c . ContentWithoutEncodingSpecified ( ) ) . ShouldReturnContent ( encoding : ControllerResultTestController . TextualContentEncoding ) ) ;
851+
852+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected encoding to be equal to {0}, but instead was null." , ControllerResultTestController . TextualContentEncoding . EncodingName ) ) ) ;
853+ }
854+
855+ #endregion
773856 }
774857}
0 commit comments