【发布时间】:2014-02-23 15:23:13
【问题描述】:
我想对不返回任何内容的方法进行单元测试。我为此使用 xUnit。我在 Google 中搜索,但我看到的每个方法都返回了一些东西。
这是我的代码:
我的班级:
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();
}
}
我的测试:
[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());
//???????????????
}
我需要测试Update 类的ShopRepository 方法,以确保调用base.Update(entity);。但是不明白怎么做。
我正在使用:
- Visual Studio 2013 Ultimate。
- 只是模拟 2013.3.1015
- xUnit 1.9.2
【问题讨论】:
标签: c# .net unit-testing mocking xunit