Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC利用测试控制器或测试模型_Asp.net Mvc_Unit Testing_Model View Controller - Fatal编程技术网

Asp.net mvc ASP.NET MVC利用测试控制器或测试模型

Asp.net mvc ASP.NET MVC利用测试控制器或测试模型,asp.net-mvc,unit-testing,model-view-controller,Asp.net Mvc,Unit Testing,Model View Controller,在一个新的MVC应用程序中,我正在构建所有的模型、控制器、视图等,而没有后端数据库设置。我有一个想法,什么样的支持将看起来像,但现在我把重点放在应用程序上 我知道我可以在控制器内模拟虚拟模型,如: public ActionResult Pictures() { MyMVCApp.Models.Pictures pics = null; MyMVCApp.Classes.Picture pic1 = new MyMVCApp.Classes.Picture {

在一个新的MVC应用程序中,我正在构建所有的模型、控制器、视图等,而没有后端数据库设置。我有一个想法,什么样的支持将看起来像,但现在我把重点放在应用程序上

我知道我可以在控制器内模拟虚拟模型,如:

public ActionResult Pictures()
{
    MyMVCApp.Models.Pictures pics = null;
    MyMVCApp.Classes.Picture pic1 = new MyMVCApp.Classes.Picture
    {
        AlbumID=1,
        Description="John Doh",
        ThumbnailLocation = "Photos/Thumbnails/John.jpg"
    };
    MyMVCApp.Classes.Picture pic2 = new MyMVCApp.Classes.Picture
    {
        AlbumID = 2,
        Description = "Jane Doh",
        ThumbnailLocation = "Photos/Thumbnails/Jane.jpg"
    };
    pics = new Pictures
    {
        PageTitle="PHOTO ALBUMS",
        PhotoAlbums = new List<MyMVCApp.Classes.PhotoAlbum>()
    };
    pics.PhotoAlbums.Add(new MyMVCApp.Classes.PhotoAlbum
    {
        AlbumID = 1,
        AlbumName = "Test1",
        AlbumCover = pic1,
        Created = DateTime.Now.AddDays(-15)
    });

    pics.PhotoAlbums.Add(new MyMVCApp.Classes.PhotoAlbum
    {
        AlbumID = 2,
        AlbumName = "Test2",
        AlbumCover = pic2,
        Created = DateTime.Now.AddDays(-11).AddHours(12)
    });
    return View(pics);
}
public ActionResult图片()
{
MyMVCApp.Models.picts pics=null;
MyMVCApp.Classes.Picture pic1=新的MyMVCApp.Classes.Picture
{
AlbumID=1,
Description=“John Doh”,
ThumbnailLocation=“Photos/Thumbnails/John.jpg”
};
MyMVCApp.Classes.Picture pic2=新的MyMVCApp.Classes.Picture
{
AlbumID=2,
Description=“Jane Doh”,
ThumbnailLocation=“Photos/Thumbnails/Jane.jpg”
};
图片=新图片
{
PageTitle=“相册”,
相册=新列表()
};
pics.PhotoAlbums.Add(新的MyMVCApp.Classes.PhotoAlbum
{
AlbumID=1,
AlbumName=“Test1”,
AlbumCover=pic1,
Created=DateTime.Now.AddDays(-15)
});
pics.PhotoAlbums.Add(新的MyMVCApp.Classes.PhotoAlbum
{
AlbumID=2,
AlbumName=“Test2”,
AlbumCover=pic2,
Created=DateTime.Now.AddDays(-11).AddHours(12)
});
返回视图(pics);
}
这样做至少可以让我在视图上看到一些东西。我担心的是,当我准备好为我的模型实际使用DB时,我不想失去我的测试模型


<> P>我应该如何分离,以便不必每次在实际控制器和测试控制器之间改变视图?

< P>您可以考虑不在控制器中设置这些数据类。而是从InMemoryPictureRepository请求它们,该存储库将在您需要它们进行测试时将实例返回给您

换句话说,将数据持久性的责任放在IRepository接口后面。这样,您就可以拥有用于测试的版本,这些版本基本上提供了硬编码的实例

最后,我认为您确实希望将依赖项注入与IoC容器一起使用,而不是在控制器中直接引用存储库,但过于简单的外观可能是这样的:

public class PictureController : Controller
{
    IPictureRepository _pictureRepository;

    public PictureController()
     {
        //Assume you change this for test/prod. Again you'd probably 
         //want to inject this if you really want testable controllers
        IPictureRepository _pictureRepository = new InMemoryPictureRepository();
     }

     public ActionResult Pictures()
     {
         List<Picture> pics  = _pictureService.GetAllPictures();
         return View(pics);
     }
}
公共类PictureController:控制器
{
i结构存储库(图片存储库);;
公共图片控制器()
{
//假设您将此更改为测试/生产。同样,您可能会
//如果你真的想要可测试的控制器,你想注入这个吗
IPactureRepository_pictureRepository=新的InMemoryPictureRepository();
}
公众行动结果图片()
{
List pics=_pictureseservice.GetAllPictures();
返回视图(pics);
}
}
现在,您可以在MemoryPictureRepository中使用此功能

public class InMemoryPictureRepository : IPictureRepository
{
    public List<Picture> GetAllPictures()
    {
        //All your hard-coded stuff to return dummy data;
    }
}
MemoryPictureRepository中的公共类:IPictureRepository
{
公共列表GetAllPictures()
{
//你所有的硬编码的东西返回虚拟数据;
}
}
这是你的生活用品:

public class PictureRepository : IPictureRepository
{
    public List<Picture> GetAllPictures()
    {
       //Code to get data from L2S or wherever. This returns real stuff
    }
}
公共类PictureRepository:IPactureRepository
{
公共列表GetAllPictures()
{
//从L2S或任何地方获取数据的代码。这将返回真实的内容
}
}