简体   繁体   中英

How to Test Void Method using xUnit?

I am want to do a unit test of a method that return nothing. I am using xUnit for this. I search in Google but every where I saw methods are returning something.

Here is my code :

My Class :

public class ShopRepository : BaseRepository<ShopInformation>
{
    public ShopRepository(IDbContext dbContext)
        : base(dbContext)
    {
    }

    public override void Update(ShopInformation entity)
    {
        if(GetAll().Any())
            base.Update(entity);
        else
         base.Add(entity);
    }

    public ShopInformation ShopInformation()
    {
        return GetAll().FirstOrDefault();
    }
}

My Test :

    [Fact]
    public void GetAllTest()
    {
        var data = new List<ShopInformation>
        {
            new ShopInformation { Name = "BBB" },
            new ShopInformation { Name = "ZZZ" },
            new ShopInformation { Name = "AAA" },
        }.AsQueryable();

        var mockSet = Mock.Create<DbSet<ShopInformation>>();
        Mock.Arrange(() => ((IEnumerable<ShopInformation>)mockSet).GetEnumerator()).Returns(data.GetEnumerator);

        Mock.Arrange(() => ((IQueryable<ShopInformation>)mockSet).Provider).Returns(data.Provider);

        Mock.Arrange(() => ((IQueryable<ShopInformation>)mockSet).Expression).Returns(data.Expression);
        Mock.Arrange(() => ((IQueryable<ShopInformation>)mockSet).ElementType).Returns(data.ElementType);
        Mock.Arrange(() => ((IQueryable<ShopInformation>)mockSet).GetEnumerator()).Returns(data.GetEnumerator());
        var interDbContext = Mock.Create<IDbContext>();
        interDbContext.Arrange(x => x.Set<ShopInformation>()).Returns(mockSet);

        var companyRepository = new ShopRepository(interDbContext);

        companyRepository.Update(new ShopInformation());

       //???????????????

    }

I need to test Update Method of ShopRepository Class to ensure that base.Update(entity); is call. But don`t understand How to do it.

I am using :

  • Visual Studio 2013 Ultimate.
  • Just Mock 2013.3.1015
  • xUnit 1.9.2

There is 2 possible solutions:

  1. If Update method modifies any value from ShopRepository class, you should perform some validation on them.

  2. Mock out base.Update method to return "expected" and assert based on that.

Your test is named GetAllTest() , so it probably shouldn't be testing the Update() function.

Best way to do this is to return a bool for if it updated or not.

public bool Update(ShopInformation entity)
{
    if(GetAll().Any())
    {
        base.Update(entity);
        return true; // True because it updated
    }
    else
    {
         base.Add(entity);
         return false; // False because it didn't
    }
}

And then your test can Assert the expected answer.

// ... Setup test code
var hasUpdated = companyRepository.Update(new ShopInformation());
Assert.Equal(true, hasUpdated);

A point to note : If you are using a Repository, you should probably use Save as this will update changes made, but will also add new records if the record does not already exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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