????????????Moq??EF??DbSet????Mock
???????????? ???????[ 2016/2/29 14:10:02 ] ??????????????? ???????
???????????
?????????mock??????????и??????????msdn??????????????mock????????? Testing with a mocking framework ??????????????mock??
????mock??????????????????IDbAsyncEnumerator??IDbAsyncEnumerable??IDbAsyncQueryProvider ??
1??TestDbAsyncEnumerator ??? IDbAsyncEnumerator
public class TestDbAsyncEnumerator<T> : IDbAsyncEnumerator<T>
{
private readonly IEnumerator<T> _inner;
public TestDbAsyncEnumerator(IEnumerator<T> inner)
{
_inner = inner;
}
public void Dispose()
{
_inner.Dispose();
}
public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
{
return Task.FromResult(_inner.MoveNext());
}
public T Current
{
get { return _inner.Current; }
}
object IDbAsyncEnumerator.Current
{
get { return Current; }
}
}
2??TestDbAsyncEnumerable ??? IDbAsyncEnumerable
public class TestDbAsyncEnumerable<T> : EnumerableQuery<T>?? IDbAsyncEnumerable<T>?? IQueryable<T>
{
public TestDbAsyncEnumerable(IEnumerable<T> enumerable)
: base(enumerable)
{ }
public TestDbAsyncEnumerable(Expression expression)
: base(expression)
{ }
public IDbAsyncEnumerator<T> GetAsyncEnumerator()
{
return new TestDbAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
}
IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
{
return GetAsyncEnumerator();
}
IQueryProvider IQueryable.Provider
{
get { return new TestDbAsyncQueryProvider<T>(this); }
}
}
3??TestDbAsyncQueryProvider ??? IDbAsyncQueryProvider
public class TestDbAsyncQueryProvider<TEntity> : IDbAsyncQueryProvider
{
private readonly IQueryProvider _inner;
public TestDbAsyncQueryProvider(IQueryProvider inner)
{
_inner = inner;
}
public IQueryable CreateQuery(Expression expression)
{
return new TestDbAsyncEnumerable<TEntity>(expression);
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
return new TestDbAsyncEnumerable<TElement>(expression);
}
public object Execute(Expression expression)
{
return _inner.Execute(expression);
}
public TResult Execute<TResult>(Expression expression)
{
return _inner.Execute<TResult>(expression);
}
public Task<object> ExecuteAsync(Expression expression?? CancellationToken cancellationToken)
{
return Task.FromResult(Execute(expression));
}
public Task<TResult> ExecuteAsync<TResult>(Expression expression?? CancellationToken cancellationToken)
{
return Task.FromResult(Execute<TResult>(expression));
}
}
???????????mock????
????var mockUnitOfWork = new Mock<IUnitOfWork>();
????mockUnitOfWork.Setup(u => u.Set<BlogCategory>()).Returns(blogCategories);
???????????????
????#region mockSet
????var mockSet = new Mock<DbSet<BlogCategory>>();
????mockSet.As<IDbAsyncEnumerable<BlogCategory>>()
????.Setup(m => m.GetAsyncEnumerator())
????.Returns(new TestDbAsyncEnumerator<BlogCategory>(blogCategories.GetEnumerator()));
????mockSet.As<IQueryable<BlogCategory>>()
????.Setup(m => m.Provider)
????.Returns(new TestDbAsyncQueryProvider<BlogCategory>(blogCategories.Provider));
????mockSet.As<IQueryable<BlogCategory>>().Setup(m => m.Expression).Returns(blogCategories.Expression);
????mockSet.As<IQueryable<BlogCategory>>().Setup(m => m.ElementType).Returns(blogCategories.ElementType);
????mockSet.As<IQueryable<BlogCategory>>().Setup(m => m.GetEnumerator()).Returns(blogCategories.GetEnumerator());
????#endregion
????var mockUnitOfWork = new Mock<IUnitOfWork>();
????mockUnitOfWork.Setup(u => u.Set<BlogCategory>()).Returns(mockSet.Object);
???????????mock??DbSet??????????????????????????
????1 passed?? 0 failed?? 0 skipped?? took 2.75 seconds (xUnit.net 1.9.2 build 1705).
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
iOS???????mocha??chai??sinon??istanbul???????????????????????????????????????д?????Java????????7??????Android?????????Robolectric3.0????(?)???Kiwi?????????????????????????????????Python?????????????????????????????????????Controller????????д?????????10???????????????????Angular????????????Component??Directive??Pipe ???ServiceAndroid????????????????????????????????--Mockito??????iOS UnitTest???????Vue?????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????