Hi,
I have created a mockDbSet as
var mockSet = Substitute.For
and I have a function GetDbSetData(), which returns List Of MyEntity.
And DbContext as dbContext = Substitute.For
dbContext.MyTable.Returns(mockSet);
When I debug this code and check dbContext, I am getting MyEntity's Result view as Empty: "Enumeration yielded no results".
Can anyone explain what should I do?
Updated:
```c#
public class RepoTest
{
public IMyDbContext _DbContext;
public IConfigurationRoot _configuration { get; set; }
protected ILogger _logger { get; set; }
public IRepository Repository { get; set; }
public RepoTest()
{
var data = GetDbSetData();
var mockSet = Substitute.For
mockSet.AddRange(data);
_DbContext = Substitute.For
_DbContext.RAActionStatusHistory.Returns(mockSet);
_configuration = Substitute.For
_logger = Substitute.For
Repository = new Repository(_DbContext,_configuration,_logger);
}
public List
{
return new List
{
new MyEntity
{
// Setting Properties
},
new MyEntity
{
// Setting Properties
}
};
}
[Fact]
public async Task GetMarksById_nsubstitute()
{
try
{
//input
id=1;
//action
int results1 = await Repository.GetDataById(id).ConfigureAwait(false);
//assert
Assert.Equal(10, results1);
}
catch
{
throw;
}
}
}
```
Could you please provide us with a Minimal, Complete, and Verifiable example of code that helps to reproduce the scenario?
Hi @surya19876,
Have you seen MSDN: Entity Framework Testing with a Mocking Framework (EF6 onwards)? I don't have experience with EF so I can't be of much help, but that article seems to be a great starting point.
Hi @zvirja, I have updated the above comment and provided the code. Please have a look.
Hi @dtchepak, I have seen this article but was not helpful as I am using .net core and entity framework core.
@surya19876 I'm also using .NET Core (2.0) with entity framework and this is what I did in order to get what you want:
https://gist.github.com/MaLiN2223/65c49045646ec771316093d718dc462f I hope it will help
Thanks @MaLiN2223 ! 馃槃
Closing this after @MaLiN2223's answer. Please re-open if you need more information.
@surya19876 I'm also using .NET Core (2.0) with entity framework and this is what I did in order to get what you want:
https://gist.github.com/MaLiN2223/65c49045646ec771316093d718dc462f I hope it will help
@MaLiN2223
Is it just me or is this no longer working with ef core latest. set.AsNoTracking().Returns(data) throws.. and cannot perform much of anything on the mock :-/
Hi @generik0,
I can't help directly as I've no EF experience sorry.
I did have a quick search around and found a couple of things that might help (depending on the exception you're getting):
Hope that helps put you on the right track. If no one chimes in on this issue maybe head to StackOverflow and ask with the [entity-framework] tag so EF devs can find it.
Regards,
David
Most helpful comment
@surya19876 I'm also using .NET Core (2.0) with entity framework and this is what I did in order to get what you want:
https://gist.github.com/MaLiN2223/65c49045646ec771316093d718dc462f I hope it will help