When to specify certain Setups in Moq

Matt M

I'm trying to follow this Get Started example for testing with Moq. I'm able to duplicate the examples within my own testing project and can get my tests to pass (testing my service where my context is injected). However, what I don't understand is WHEN to use each of the following Setup calls:

var mockSet = new Mock<DbSet<Blog>>(); 
mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider); 
mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression); 
mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType); 
mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); 

Can someone explain in very basic terms as to when each of these should be used?

For example, It seems that if the method in my service that I'm testing uses an expression, I need to do the 2nd setup call above (I've done some trial and error by removing and re-inserting these calls). I've been to the Moq documentation as well as MSDN for Table-TEntity and I still don't see it. Perhaps because I don't have a strong grasp of the Linq namespace.

StuartLC

TL;DR - When using an Entity Framework DBContext dependency, you will need perform these Setups on any DBSet which you intend to mock, specifically to return fake data to any LINQ queries on the DBSet. All 4 setups should be done for each mocked DbSet - this can be done generically in a helper method.

In more Detail:

In general, with Strict mode off, Setup is only required on methods that you actually want to Mock. In this case, if you haven't done a Setup on a method which is invoked during your Unit Test, Moq will instead provide default behaviour for any method which hasn't been explicitly Setup, which typically is to return the default(T) of any expected return type, T. For classes, the default is null, which isn't really going to help any during testing of classes dependent on a Mocked EF DbContext.

The specific example you have provided is the standard mocked setup for an Entity Framework DbSet, which then allows you to provide fake data for this specific DbSet (DbSet<Blog>), by providing an alternative IQueryable<Blog> from a List<Blog> collection (as opposed to the usual concrete RDBMS implementation).

A suggestion would be to move the DbSetmock code into your standard unit test plumbing setup framework / toolkit, to create a helper method like:

public static Mock<IDbSet<T>> GetMockedDbSet<T>(IList<T> fakeData) where T : class, new()
{
    var data = fakeData.AsQueryable();

    var mockSet = new Mock<IDbSet<T>>(); 
    mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider); 
    mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression); 
    mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType); 
    mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

    return mockSet;
}

Which you can then set up on your Mock DBContext, as follows:

var mockContext = new Mock<IMyDbContext>();
var mockBlogDbSet = GetMockedDbSet<Blog>(new List<Blog>{... fake data here ...});
mockContext.Setup(c => c.Blogs).Returns(mockBlogDbSet.Object);

var sut = new SomeClassIWantToTest(mockContext.Object); // Inject dependency into Ctor
sut.DoSomething();...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When to specify certain Setups in Moq

From Dev

Struggling with Moq: The following setups were not matched

From Dev

Moq Error : Moq.MockVerificationException: The following setups were not matched

From Dev

Moq Error : Moq.MockVerificationException: The following setups were not matched

From Dev

MOQ error setups not matched with Async / Await Unit Test

From Dev

How to specify that a function returns a certain type when new'ed in TypeScript?

From Dev

Specify width of certain columns

From Dev

Specify hinting for a certain font

From Dev

Specify hinting for a certain font

From Dev

Specify a certain column in a range?

From Dev

Specify Specific Identity file when ssh'ing as certain user in ~/.ssh/config

From Dev

Is there a function to specify a message when reading in a file if the line doesn’t follow a certain pattern

From Dev

Specify Specific Identity file when ssh'ing as certain user in ~/.ssh/config

From Dev

Mock certain part of the method using Moq

From Dev

Mock certain part of the method using Moq

From Dev

Moq verify fails when SelectMany

From Dev

Specify styling (font, in particular) for a certain cell in Prawn

From Dev

How to specify a flex item to be of certain minimum width?

From Dev

Specify certain values with Facebook Graph request

From Dev

How to specify a certain csv in the errorbar line

From Dev

Can I specify certain files for Dropbox to ignore?

From Dev

specify a time interval in which to execute a certain script

From Dev

How to specify a certain number of fft points in a frequency

From Dev

how to specify /schedule a certain task in laravel based on certain day

From Dev

Moq Unit test fails when calling a Service

From Dev

Moq object not working when executing a secondary command

From Dev

When should I use the .As method of Moq?

From Dev

Moq Unit test fails when calling a Service

From Dev

Specify port when using snmptrap

Related Related

  1. 1

    When to specify certain Setups in Moq

  2. 2

    Struggling with Moq: The following setups were not matched

  3. 3

    Moq Error : Moq.MockVerificationException: The following setups were not matched

  4. 4

    Moq Error : Moq.MockVerificationException: The following setups were not matched

  5. 5

    MOQ error setups not matched with Async / Await Unit Test

  6. 6

    How to specify that a function returns a certain type when new'ed in TypeScript?

  7. 7

    Specify width of certain columns

  8. 8

    Specify hinting for a certain font

  9. 9

    Specify hinting for a certain font

  10. 10

    Specify a certain column in a range?

  11. 11

    Specify Specific Identity file when ssh'ing as certain user in ~/.ssh/config

  12. 12

    Is there a function to specify a message when reading in a file if the line doesn’t follow a certain pattern

  13. 13

    Specify Specific Identity file when ssh'ing as certain user in ~/.ssh/config

  14. 14

    Mock certain part of the method using Moq

  15. 15

    Mock certain part of the method using Moq

  16. 16

    Moq verify fails when SelectMany

  17. 17

    Specify styling (font, in particular) for a certain cell in Prawn

  18. 18

    How to specify a flex item to be of certain minimum width?

  19. 19

    Specify certain values with Facebook Graph request

  20. 20

    How to specify a certain csv in the errorbar line

  21. 21

    Can I specify certain files for Dropbox to ignore?

  22. 22

    specify a time interval in which to execute a certain script

  23. 23

    How to specify a certain number of fft points in a frequency

  24. 24

    how to specify /schedule a certain task in laravel based on certain day

  25. 25

    Moq Unit test fails when calling a Service

  26. 26

    Moq object not working when executing a secondary command

  27. 27

    When should I use the .As method of Moq?

  28. 28

    Moq Unit test fails when calling a Service

  29. 29

    Specify port when using snmptrap

HotTag

Archive