Comments (7)
@risogolo thanks for the issue. I'm not sure that _uow.Setup(x => x.Context.Set<Country>().FindAsync(1)).ReturnsAsync(new Country { Id:1 })
is proper way how FindAsync should be configured. I would suggest you more carefully have a look similar issue #7 with code example.
in short, try to set up FindAsync for countries
instead of _uow
. Please let me know if it helped.
from mockqueryable.
Im sorry it still does not work on my side
private Mock<IOrderManagerUnitOfWork<OrderManagerContext>> _uow = new Mock<IOrderManagerUnitOfWork<OrderManagerContext>>();
var countries = CountryStub.GetCountries().AsQueryable().BuildMockDbSet(); // GetCountries()r returns List<Country>
countries.Setup(x=>x.FindAsync(1)).ReturnsAsync((object[] ids) =>
{
return countries.Object.FirstOrDefault(x => x.Id == 1);
}); //also why I should do this, Im basically mocking the data (DbSet), so I expect the linq extension methods will work on mocked collection
_uow.Setup(x => x.Context.Set<Country>()).Returns(countries.Object);
var repository = new CountryRepository(_uow.Object); //passing context containing mocked DbSet wrapped in UOW
var result = await repository.FindByIdAsync(1); //internally calls FindAsync(id)
Assert.IsNotNull(result); //still null
from mockqueryable.
@risogolo instead of return countries.Object.FirstOrDefault(x => x.Id == 1);
I would suggest CountryStub.GetCountries().FirstOrDefault(x => x.Id == 1);
because FindAsync should return items from real data, you can also see it in my example #7
Not sure if the line will work:
_uow.Setup(x => x.Context.Set<Country>()).Returns(countries.Object);
not sure if the part of code x.Context.Set<Country>()
will return you your mock object. I would suggest you to implement IOrderManagerUnitOfWork in a test class and then use it in your tests. Something like this:
internal class MyTestClass: IOrderManagerUnitOfWork<OrderManagerContext>{
DbSet<Country> _countryDbset;
public MyTestClass(DbSet<Country> countryDbset){
_countryDbset = countryDbset;
}
...
public Set<Country>
{
get{
return _countryDbset;
}
}
...
}
from mockqueryable.
My intention was to mock data in DbSet, not FindAsync method, I expected that FindAsync will return the data from mocked DbSet.
Anyway, so the first suggestion to return CountryStub.GetCountries().FirstOrDefault(x => x.Id == 1);
instead does not work and this
_uow.Setup(x => x.Context.Set<Country>()).Returns(countries.Object);
for getting the list from repository works correctly for the tests where I'm testing collections to be returned from repository method, but not for the FindAsync
from mockqueryable.
but this works
countries.Setup(x => x.FindAsync(It.IsAny<object[]>())).ReturnsAsync((object[] ids) =>
{
return CountryStub.GetCountries().FirstOrDefault(x => x.Id == 1);
});
but this is not
object[] args = new object[1];
args[0] = 1;
countries.Setup(x => x.FindAsync(args)).ReturnsAsync((object[] ids) =>
{
return CountryStub.GetCountries().FirstOrDefault(x => x.Id == 1);
});
from mockqueryable.
If you can provide full code, I'll help you more. but looks like the issue is not related to the project.
from mockqueryable.
@risogolo FindAsync(It.IsAny<object[]>()
works as expected for me.
var mock = users.AsQueryable().BuildMockDbSet();
mock.Setup(x => x.FindAsync(It.IsAny<object[]>())).ReturnsAsync((object[] ids) =>
{
var id = (Guid) ids.First();
return users.FirstOrDefault(x => x.Id == id);
});
I have added a unit test for FindAsync
case as example: DbSetFindAsyncUserEntity
from mockqueryable.
Related Issues (20)
- After upgrade it does not work with EF.Functions.Like HOT 1
- Unable to mock result of IAsyncEnumerable from DbSet using NSubstitute HOT 2
- Write Entities HOT 2
- Parameterized Repository Query Issue HOT 1
- Set return value of FirstOrDefaultAsync method by NSubstitute returns error HOT 2
- Document usage without mocking framework? HOT 3
- ToListAsync() returns empty list after package update to 6.0.0 HOT 3
- how would I get firstordefaultasync working? HOT 1
- Typo in About section for project HOT 1
- Mocking AddAsync HOT 1
- Cannot verify "Remove" method called on EF HOT 1
- ToListAsync() returns empty list on second call HOT 3
- Return value of SaveChangesAsync HOT 1
- Not suported with EF7: ExecuteDeleteAsync and ExecuteUpdateAsync HOT 5
- Not suported with EF6: BulkUpdateAsync, BulkDeleteAsync, SingleUpdateAsync. HOT 1
- Modifying base collection throws System.InvalidOperationException HOT 3
- Does mocking SingleOrDefaultAsync not work for FakeItEasy ? HOT 1
- Supporting `EF.Functions.Like` and/or prossibility to provide cusotm `ExpressionVisitor` HOT 1
- Move BuildMock to MockQueryable.EntityFrameworkCore
- Queryable filters are not applied to inmemory datasets
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockqueryable.