✅ System.NullReferenceException : Object reference not set to an instance of an object.
Hey guys, fairly new to .NET C# here. I'm trying to learn Unit Testing/Mocking at the moment. I found myself stuck on this error for days now that I'm thinking of just skipping it over and move on to another topic, but I really really wanna resolve this issue!
I setup a passing test but it's returning me an error 'System.NullReferenceException : Object reference not set to an instance of an object.' I'm so lost here, I've tried everything but nothing worked.
Please see photos for reference. I included the method I'm testing, mocking and the test result.
19 Replies
u are mocking the
IDBContext.GetEmployeeDetails(int)
method, but ur EmployeeRepository
doesnt even use that method in its GetEmployeeDetails(int)
method.
u would have to mock everything _context
related from this method:but this will be a pain, it would be easier to set up an in-memory db and seed it with the data u need and drop the in-memory db afterwards again
basically u would have to mock
_context.Employees
then on that result u would have to mock .Include(j => j.jobPosition)
then on that result u would have to mock .Include(d => d.department)
then on that result u would have to mock .Select(emp => new EmployeeDetails(...))
then on that result u would have to mock .Where(e => e.EmployeeId == id)
then on that result u would have to mock .FirstOrDefaultAsync()
so setting up the in-memory db and adding the data would be quite easier and less error pronedoes this mean im mocking the wrong method?
you're right, i came across in-memory db upon searching for solutions for this problem and it seemed to be the best option for me
but i still somehow wanna learn mocking first before i hop on to another topic
feels bad skipping it over
basically what would be easy to mock would be the use of ur
EmployeeRepository
if it implements an interface
for example assume this little controller:
to test this, u would mock the IEmployeeRepository
:
yes, because u didnt use the IDBContext.GetEmployeeDetails(int)
method in ur EmployeeRepository.GetEmployeeDetails(int)
method, but the EF core stuff directly.
so u would have to mock all of that chain as well
it would ofc be totally valid to mock the ef core part, but as shown, its relatively complex, so its time consuming and error prone.
so use the right tool for the job
especially stateful things and chain calls are not easy to mock. there its sometimes even simplier to implement the interface urself instead of using a mocking library.
most of the time u use the latter only for simple stuff, where u dont have to mock much, eg a datetime provider or like the example shown mocking the employee repository itself.yes my
EmployeeRepository
does implement an interface
it looks like thisthe interface is like this
yeah then my mock example would fit it
and generally the use of
IEmployeeRepository
is easily mockable, as u do not have any complex call chains on it, unlike the EF core stuff
so basically to test the repository implementation, use an in memory db provider
to test the controllers, mock the repositorythanks a lot man
it's all clearer to me now
been stuck on this for days
glad i could help o7
searching for answers
thanks again!
btw, i assume u r using Moq as mocking library right now?
yes
BleepingComputer
Popular open source project Moq criticized for quietly collecting data
Open source project Moq (pronounced "Mock") has drawn sharp criticism for quietly including a controversial dependency in its latest release. Moq's 4.20.0 release from this week included another project, SponsorLink, which caused an uproar among open source software consumers, who likened the move to a breach of trust.
iirc NSubstitute is the new drop in for Moq now https://nsubstitute.github.io/
damn
guess i should switch
just to be safee
yeah, sad story =/
especially in EU this could lead to legal issues so for me its not an option to use Moq anymore as long as this stuff stays in the source.
and the trust is gone that it wont reappear
glad i use a dummy email when practicing stuff
i have like 2389238283 emails
for things like installations/game stuff
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.