简体   繁体   English

为什么我在单元测试中有迁移问题但在控制器 c# Web API 中没有

[英]Why i have migration problem in unit test but not in controller c# Web API

Hello i have a project web api in c# and i want to write unit test to check my controller.你好,我在 c# 中有一个项目 web api,我想编写单元测试来检查我的控制器。 But i find an error that i really don't understand.但是我发现一个我真的不明白的错误。 When i run my method in controller当我在控制器中运行我的方法时

public class TherapistsController : ApiController
{
    TherapistService _therapistService = new TherapistService();
    GeneralService _generalService = new GeneralService();

    //GET: api/Therapists/GetAllTherapists
    [HttpGet]
    [Route("api/Therapists/GetAllTherapists")]
    public IHttpActionResult GetTherapist()
    {
        var therapists = _therapistService.GetAllTherapist();
        if (therapists.Count() > 0)
            return Ok(therapists);
        return NotFound();
    }
}

it give me the result and it is fine它给了我结果,很好在此处输入图像描述

But if i run this method in a unit test但是如果我在单元测试中运行这个方法

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void GetAllTherapistByOutletTest()
    {
        var therapists = new WebAPI.Controllers.TherapistsController();

        IHttpActionResult result = therapists.GetTherapist();

        Assert.IsInstanceOfType(result, typeof(OkResult));

    }
}

it give me the error它给了我错误

在此处输入图像描述

Like u see the error says that i need to update database by migration but it still give me same error after i migrate and update database.就像你看到错误说我需要通过迁移更新数据库但是在我迁移和更新数据库后它仍然给我同样的错误。 But when i run the method by calling API,it still give me the result like the first picture and no error.但是当我通过调用 API 运行该方法时,它仍然给我像第一张图片一样的结果并且没有错误。 I debug both ways and they have same steps until method GetAll() in repository like u see in the above picture.我调试了两种方式,它们具有相同的步骤,直到存储库中的方法GetAll()就像你在上图中看到的那样。 I don't really know what wrong?我真的不知道哪里错了?

Repository资料库

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    protected readonly SpaDbContext db;
    public GenericRepository(SpaDbContext _db)
    {
        this.db = _db;
    }
    public void Add(TEntity entity)
    {
        db.Set<TEntity>().Add(entity);
    }

    public void AddRange(IEnumerable<TEntity> entities)
    {
        db.Set<TEntity>().AddRange(entities);
    }

    public void Detached(TEntity entity)
    {
        db.Entry<TEntity>(entity).State = EntityState.Detached;
    }

    public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
    {
        return db.Set<TEntity>().Where(predicate);
    }    

    public TEntity Get(Expression<Func<TEntity, bool>> predicate)
    {
        return db.Set<TEntity>().FirstOrDefault(predicate);
    }

    public TEntity Get(object Id)
    {
        return db.Set<TEntity>().Find(Id);
    }

    public IEnumerable<TEntity> GetAll()
    {
        return db.Set<TEntity>().ToList();
    }

    public void Remove(TEntity entity)
    {
        db.Set<TEntity>().Remove(entity);
    }

IRepository存储库

namespace Repository
{
    public interface IGenericRepository<TEntity> where TEntity : class
    {
        IEnumerable<TEntity> GetAll();
        IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
        TEntity Get(Expression<Func<TEntity, bool>> predicate);
        TEntity Get(object Id);
        void Add(TEntity entity);
        void AddRange(IEnumerable<TEntity> entities);
        void Update(TEntity entity);

        //void Remove(object Id);
        void Remove(TEntity entity);
        void RemoveRange(IEnumerable<TEntity> entities);

        void Detached(TEntity entity);

        IEnumerable<TEntity> GetByQuery(string query);
    }
}

Make sure you have set a valid connection string in test project as web api project确保您已在测试项目中将有效的连接字符串设置为 web api 项目

If you want do a real unit test with the controller methods you need use Mock services with this you will not have this kind of problems如果你想用控制器方法做一个真正的单元测试,你需要使用模拟服务,你不会有这种问题

https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/unit-testing-controllers-in-web-api https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/unit-testing-controllers-in-web-api

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

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