Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type or namespace name Mock<> could not be found Entity Framework 6

I am trying to mock my DbContext for writing my unit tests.

I saw a tutorial, and tried to do it like the following:

[TestMethod]
public void MyFirstTest()
{
    var mockSet = new Mock<DbSet<VMStored>>();
}

But I am getting the following error:

Type or namespace name Mock<> could not be found

I am using EF 6. I know this is something simple but I didn't find any answer online regarding this issue.

I don't know what assembly if needed I need to add or what package to install.

I did everything they said in the MSDN tutorial, including update my code in the .Context.tt file of the following function:

public string DbSet(EntitySet entitySet)
{
    return string.Format(
        CultureInfo.InvariantCulture,
        "{0} virtual DbSet<{1}> {2} {{ get; set; }}",
        Accessibility.ForReadOnlyProperty(entitySet),
        _typeMapper.GetTypeName(entitySet.ElementType),
        _code.Escape(entitySet));
}

Any ideas what I need to do?

like image 641
daniel the man Avatar asked Nov 17 '16 08:11

daniel the man


People also ask

How do you fix the type or namespace name could not be found?

Solution 1 That may be it's in a different project in the current solution, in a external DLL, or just in a different namespace in another file in the current project. Start by hovering the mouse over the error, and open the drop down that appears. If it has an "include" option, click that, and it should fix it.

What is mock repository?

Mocking is a way to encapsulate your unit tests. If you want to test a service method you are not interested if the repository is working. For this you will write repository tests. Therefore you mock the repository call and tell which result should be returned to test your method in all possible situation.


1 Answers

Go to the Nuget Package Manager (right click project or solution in Solution Manager) and search for Moq. You're just missing the libraries.

like image 118
Trevor Cousins Avatar answered Oct 11 '22 22:10

Trevor Cousins