单元测试在失败时通过

时间:2017-04-22 00:10:23

标签: c# .net unit-testing moq xunit

在以下示例中,任何人都可以看到我出错的地方。我正在编写测试以检查是否已存在ID。我有一个' UserService'调用通用存储库中的方法' RentalsRepository'这让我调用数据。 *注意代码在系统中有效,而不是在我的测试中

回购

public class RentalsRepository<T> : IRentalsRepository<T> where T : BaseClass
{
    private readonly RentalsDBContext _Context;
    private DbSet<T> entities;
    string errorMessage = string.Empty;

    public RentalsRepository(RentalsDBContext _Context)
    {
        this._Context = _Context;
        entities = _Context.Set<T>();
    }

    public T Get(string Id)
    {
        return entities.SingleOrDefault(e => e.Id == Id);
    } ...

UserService

 public class UserService : IUserService {
private IRentalsRepository<UserAccount> _userRepository;

    public UserService(IRentalsRepository<UserAccount> userRepository)
    {
        this._userRepository = userRepository;
    }

    public UserAccount GetUserFromId(string id)
    {
        UserAccount user = _userRepository.Get(id);
        user.Email = Encryption.DecryptFromDatabase(user.Email);
        return user;
    }...

测试类

  [Fact]
    public void UserService_GetByID()
    {
        var users = new List<UserAccount> {
            new UserAccount { Id = "idstring1", Username = "username1"},
            new UserAccount { Id = "idstring2", Username = "username2" },
            new UserAccount { Id = "idstring3", Username = "username3" },
    }.AsQueryable();

        var mockSet = new Mock<DbSet<UserAccount>>();

        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Provider).Returns(users.Provider);
        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Expression).Returns(users.Expression);
        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.ElementType).Returns(users.ElementType);
        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

        var mockContext = new Mock<RentalsDBContext>();
        mockContext.Setup(m => m.Set<UserAccount>()).Returns(mockSet.Object);

        var mockRepo = new Mock<IRentalsRepository<UserAccount>>();
        mockRepo.Setup(m => m.Get(It.IsAny<string>())).Returns(mockSet.Object.FirstOrDefault());

        var service = new UserService(mockRepo.Object);
        UserAccount results = service.GetUserFromId("non-existant");

        Assert.Equal("idstring1", results.Id);
    } 

当我调试时,我可以看到价值&#39;&#34;不存在&#34;&#39;正在方法public UserAccount GetUserFromId(string id)中使用,但它仍以某种方式返回用户

最新尝试

        [Fact]
    public void UserService_GetUserByUsername()
    {
        byte[] b = Encryption.GetSalt();
        var users = new List<UserAccount> {
            new UserAccount { Id = "idstring2", Username = "username2" },
           new UserAccount { Id = "idstring3", Username = "username3" },
    }.AsQueryable();

        var mockContext = new Mock<RentalsDBContext>();
        var mockSet = new Mock<DbSet<UserAccount>>();

        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Provider).Returns(users.Provider);
        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.Expression).Returns(users.Expression);
        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.ElementType).Returns(users.ElementType);
        mockSet.As<IQueryable<UserAccount>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

        mockContext.Setup(m => m.Set<UserAccount>()).Returns(mockSet.Object);

        var mockRepo = new Mock<RentalsRepository<UserAccount>>(mockContext.Object);

        var testClass = new UserService(mockRepo.Object);
        UserAccount results = testClass.GetUserByUsername("username2");

        Assert.Equal("username1", results.Username);
    }

0 个答案:

没有答案