简体   繁体   English

我怎么能模拟HttpPostedFileBase mock

[英]How can i mock HttpPostedFileBase mock

How can I mock HttpPostedFileBase ? 我怎么能模拟HttpPostedFileBase?

its props are readonly and i need to test a fileupload module. 它的道具是只读的,我需要测试一个fileupload模块。

What mocking framework should i use? 我应该使用什么模拟框架?

    public class FileUpload
    {

        private readonly HttpPostedFileBase _file;
        private readonly string _filePath;

        public FileUpload(HttpPostedFileBase file, string path)
        {
            _file = file;
            _filePath = path;
        }
.......

HttpPostedFileBase is an abstract class. HttpPostedFileBase是一个抽象类。 In your test, just create a new class inheriting from it and create your own stub version (hardcoding the method results you need to test). 在您的测试中,只需创建一个继承自它的新类并创建您自己的存根版本(对您需要测试的方法结果进行硬编码)。 What style of testing are you doing? 你在做什么样的测试?

  • mocking (testing for interaction) 嘲笑(测试交互)
  • stubbing (testing with known values) 存根(使用已知值进行测试)
  • fakes (using simpler objects to accomplish the task, not necessarily production code). 假货(使用更简单的对象来完成任务,不一定是生产代码)。

I think here stubbing would be most appropriate. 我认为这里的存根是最合适的。

Alternatively, you could use Moq (or another framework) to help with this. 或者,您可以使用Moq(或其他框架)来帮助解决此问题。

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

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