简体   繁体   English

FileContentResult null从控制器方法

[英]FileContentResult null from Controller method

I have the following method in my Controller; 我的控制器中有以下方法;

        [HttpGet]
        public ActionResult Process(int id)
        {
                var file = ServiceUtilities.FileManager.GetFile(id);

                if (file != null)
                {
                    var fcr = new FileContentResult(new byte[1], @"Image\Gif");
                    return fcr;
                }

                return RedirectToAction("Index", "Error");
        }

And I am calling this code from my Unit Test as follows; 我从单元测试中调用此代码,如下所示;

var response =
            new Controllers.FileController().Process(1) as FileStreamResult;


Assert.IsNotNull(response);

However, response is always null. 但是,响应始终为空。 If I set a breakpoint on fcr in Controller code, it is not null. 如果我在Controller代码中的fcr上设置了一个断点,则它不为null。

For clarification, file is a mocked object and is not null. 为了澄清起见, file是模拟对象,并且不为null。

I may be missing something, but FileContentResult (what you return from your method) is NOT a FileStreamResult (what you cast it to in your test). 我可能丢失了一些内容,但是FileContentResult (从方法返回的内容)不是FileStreamResult (在测试FileContentResult转换为的内容)。 They both inherit from FileResult , but in order for your test to pass, FileContentResult would need to inherit from FileStreamResult . 它们都继承自FileResult ,但是为了使测试通过, FileContentResult需要继承自FileStreamResult

Or is that a mis-type? 还是那是错误的类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM