Question

gib898 on Thu, 18 May 2017 19:47:58


Hello,

I'm having problem with my unit tests. Things aren't working in my unit tests as they usually do when running my application.

First of all, let me explain the structure of my data.

We have a Project. Under the Project, we have something called ProjectRecommendations. Under each ProjectRecommendation is a list of Recommendations. <-- These are all entities in EntityFramework.

All this information is parsed in from a CSV file.

In the code, we have this line which does the parsing of the file:

var nodes = ParseData(progress);

Further down in the same function, we have this:

var projectEntry = Context.Entry(project);

Before this point, Project.ProjectRecommendations.ElementAt(x).Recommendations.Count = 0.

After this point, Project.ProjectRecommendations.ElementAt(x).Recommendations.Count > 0.

(where x is any number between 0 and Project.ProjectRecommendations.Count.)

In other words, calling Context.Entry(project) seems to add the Recommendations to the ProjectRecommendations.

This is the case except for when I'm running my unit tests. When I run my unit tests, Project.ProjectRecommendations.ElementAt(x).Recommendations.Count = 0 before and after the call to Context.Entry(project).

I wonder if it has something to do with Ids. I notice that when I'm running my application, the ProjectRecommendations all have database Ids, whereas they do not when running my unit tests.

Does anybody know what's going on here? Does anybody know how to call Context.Entry(project) during unit tests such that I get all my Recommendations under my ProjectRecommendations?

Sponsored



Replies

Wendy Zang on Fri, 19 May 2017 06:48:07


Hi gib898,

Thank you for posting here.

According to your question is more related to Unit Testing, I will move it to  Visual Studio Unit Testing forum for suitable support.

The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

Thanks for your understanding and cooperation.

Best Regards,

Wendy 

Fletch Zhou on Tue, 23 May 2017 08:36:27


Hi friend, 

>> All this information is parsed in from a CSV file.

If you are using .CSV file in Unit test, you need add the data source attribute above your test code. 

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
    "|DataDirectory|\\data.csv", "data#csv",
    DataAccessMethod.Sequential), DeploymentItem("data.csv"),
    TestMethod]

this page shows the details of different data source to each type of file: 

https://blogs.msdn.microsoft.com/mathew_aniyan/2009/03/16/data-driving-coded-ui-tests/

and to take data from data from CSV file, I always use

TestContext.DataRow["ValueOne"].ToString();

other than Context.Entry(which seem more related to Entity Frame). 

Hope it helps.

Best regards, 

Fletch


MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


gib898 on Wed, 24 May 2017 18:13:41


Thanks Fletch,

The application reads from csv files, but for the unit tests, the data is just mocked (no csv).

Fletch Zhou on Fri, 09 Jun 2017 02:13:11


Hi friend,

It seems it's more related to EF, or mocking the Entry () ? And similar thread here might offer you some ideas:

https://stackoverflow.com/questions/25768459/unit-test-failing-on-ef-entry-state

Best regards,

Fletcher 


MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.