in Education by
I spent an evening trying to mock an object that implements IQueryable: public interface IRepo : IQueryable { } The best I could come up with is something like this: var items = new Item[] {}.AsQueryable(); var repo = new Mock(); repo.Setup(r => r.GetEnumerator()).Returns(items.GetEnumerator()); repo.Setup(r => r.Provider).Returns(items.Provider); repo.Setup(r => r.ElementType).Returns(items.ElementType); repo.Setup(r => r.Expression).Returns(items.Expression); Is there a more concise way to do the same? It would be easier to expose a property/method in IRepo that returns IQueryable and the simply mock like this: repo.Setup(r => r.GetItems()).Returns(new Items[]{ }.AsQueryable()); But this is not what I want to do =) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This is nothing new, just a cleaner way of doing it. I also have repositories where the repository itself is also an IQueryable, so I needed the same thing. I basically just put your code into an extension method like this at the root level of my test project, to make it available to all tests: public static class MockExtensions { public static void SetupIQueryable(this Mock mock, IQueryable queryable) where T: class, IQueryable { mock.Setup(r => r.GetEnumerator()).Returns(queryable.GetEnumerator()); mock.Setup(r => r.Provider).Returns(queryable.Provider); mock.Setup(r => r.ElementType).Returns(queryable.ElementType); mock.Setup(r => r.Expression).Returns(queryable.Expression); } } This basically just offers reusability, since you're likely to want to do this in several tests, and in each test it makes the intention clear and the mess minimal. :)

Related questions

0 votes
    I've isolated the behaviour into the following test case. I'd be grateful to anyone who can tell ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    What are the differences between IEnumerable and IQueryable?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    I'm trying to mock a TypeScript class with Jest and I'm obviously doing something because receive the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    How can I mock the database calls to make my application logic been tested without database? JavaScript ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    How can I mock the database calls to make my application logic been tested without database? JavaScript ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    How can I mock the database calls to make my application logic been tested without database? JavaScript ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    How can I mock the database calls to make my application logic been tested without database? JavaScript ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    Which of these is not a mocking framework? (a) EasyMock (b) Mockito (c) PowerMock (d) MockJava I had ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these is not a mocking framework? (a) EasyMock (b) Mockito (c) PowerMock (d) MockJava I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I need to implement a custom priority queue without using the PriorityQueue form Java.Util... I have three basic methods : ... what I've done so far ... public class PriorityQueue...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I write a lot of dynamically generated content (developing under PHP) and I use jQuery to add extra ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Greetings, currently I am refactoring one of my programs, and I found an interesting problem. I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    How you can implement nullable types in C#? explain with the syntax of Nullable type....
asked Jul 27, 2021 in Technology by JackTerrance
...