Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 675751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:51:11+00:00 2026-05-14T00:51:11+00:00

I am mocking a repository that should have 1 entity in it for the

  • 0

I am mocking a repository that should have 1 entity in it for the test scenario. The repository has to return this entity based on a known id and return nothing when other ids are passed in.

I have tried doing something like this:

_myRepository.Expect(item => item.Find(knownId)).Return(knownEntity);
_myRepository.Expect(item => item.Find(Arg<Guid>.Is.Anything)).Return(null);

It seems however the second line is overriding the first and the repository always returns null. I don’t want to mock all the different possible IDs asked (they could go up to hundreds) when the test scenario is only concerned with the value of one Id.

I understand that I can change the second line to be the negative of the first. However this would become more and more difficult (as well as ugly) if the repository has to return more than 1 entity for a test scenario.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:51:11+00:00Added an answer on May 14, 2026 at 12:51 am

    It did work for me correctly. My guess is that you are searching for your known id more than once. Try changing the quoted lines to:

    _myRepository.Stub(item => item.Find(knownId)).Return(knownEntity);
    _myRepository.Stub(item => item.Find(Arg<Guid>.Is.Anything)).Return(null);
    

    and let me know if it solves the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This test is to check that I can return a ViewModel object by creating
I have the following test to verify that my repository is calling it's respective
(C#, Rhino Mocks, MbUnit). I have a class called AccountManager that has a RegisterUser()
My application I have an application design that looks like this: web application layer
Have any other iPhone developers experienced this phenomenon? I can see his contributions in
I'm mocking about with plt-scheme's ffi and I have a C-function that returns a
New to Moq and Mocking in general. Testing a class that has a generic
I've seen it mentioned sometimes that Repository Pattern is built into Entity Framework Code
So I'm just getting used to mocking stuff. I have this private variable here:
I am having problem mocking an object to test a descriptor. This is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.