This is a migrated thread and some comments may be shown as answers.

Null exception from mocking Linq to sql

2 Answers 93 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 01 Mar 2020, 04:04 AM

hi mate, I was wondering if it is possible to mock the following case.

 

from school in dbContext.Schools

join student in dbContext.Students

   on school.id equals student.schoolId into students

let firstStudent = students.FirstOrDefault()

select firstStudent.Name;

 

When all the tables are empty and there is no school and no student in the tables, a real sql execution just returns an empty object of IEnuemrable<string> type. If I mock 'dbContext.Schools' and 'dbContext.Students', 'firstStudent.Name' throws a null exception because 'firstStudent' is null.

Is there a way to conditionally mock only in this context of linq to sql so that we can bypass the null exception and get the mocked execution to run smoothly?

2 Answers, 1 is accepted

Sort by
0
Brad
Top achievements
Rank 1
answered on 01 Mar 2020, 09:46 PM
correction: 'When all the tables are empty and there is no school and no student in the tables' => 'When there is ONE school and no student in the tables'
0
Ivo
Telerik team
answered on 04 Mar 2020, 09:12 AM

Hello Brad,

Could you put more details about what exactly is the use case and what you are trying to achieve, I am afraid that it is not clear enough based on the brief description. I would like to ask you to prepare a sample unit test project that reflects all these things. This would help us to understand better your scenario and greatly improve the investigation process.

Regards,
Ivo
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Brad
Top achievements
Rank 1
Answers by
Brad
Top achievements
Rank 1
Ivo
Telerik team
Share this question
or