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

  • Home
  • SEARCH
  • 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 246871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:12:31+00:00 2026-05-11T21:12:31+00:00

//IsExist always false,is it a bug? [TestMethod] public void IsExist() { private Mock<IRepository> repository

  • 0

//IsExist always false,is it a bug?

  [TestMethod]
   public void IsExist()
   {
       private Mock<IRepository> repository = new Mock<IRepository>();
                   Foo f = new Foo();
                   repository.Expect(s => s.IsExist(foo)).Returns(true);
       var controller = new MyController(repository.Object);
       var result = (ViewResult)controller.DoSometing();

   }

           public class MyController : Controller
           {

           IRepository _repository ;

           public MyController(IRepository repository)
   {
       _repository = repository;

   }

           public ViewResult DoSometing()
   {
       bool IsExist = _repository.IsExist(new Foo());
                   //IsExist always false,is it a bug?
       return View(foo);
   }

           }
  • 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-11T21:12:31+00:00Added an answer on May 11, 2026 at 9:12 pm

    Firstly, which mocking library are you using (the answer may change based on that)?

    I know if you were using Rhino Mocks, the problem would be that your expectation is set up to return true when it receives that specific instance of foo that you create at the top. This is a different instance to what is being passed in when its executed in your controller, so it returns false (since no expectation was set against that version of the foo object). To be clearer, if you had this code:

    Foo f1 = new Foo();
    Foo f2 = new Foo();
    repository.Expect(s => s.IsExist(f1)).Returns(true);
    bool b1 = repository.Object.IsExist(f1);
    bool b2 = repository.Object.IsExist(f2);
    

    I would expect that b1 would be true (since that’s the specific expectation you set up, i.e. given f1 return true) and b2 would be false (since you didn’t tell the repository to do anything specific if it received f2, so it will fall back to its default behaviour of returning false).

    In Rhino Mocks, you’d need to set up your expectation like this:

    repository.Expect(s => s.IsExist(Arg<Foo>.Is.TypeOf)).Returns(true);
    

    which would return true if IsExist was called with any instance of a Foo object. If you needed to be more specific, you could have something like this:

    repository.Expect(s => s.IsExist(f => f.SomeProperty == "blah" && f.OtherProperty.StartsWith("baz")))).Returns(true);
    

    which would return true if foo.SomeProperty equaled "blah" and foo.OtherProperty started with "baz".

    Doesn’t look like you’re using Rhino Mocks, as your syntax is a little different, but hopefully that points you in the right direction…

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

Sidebar

Ask A Question

Stats

  • Questions 154k
  • Answers 154k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to analyze a 64-bit process dump then… May 12, 2026 at 10:31 am
  • Editorial Team
    Editorial Team added an answer This doesn't answer your entire question, but when Google Reader… May 12, 2026 at 10:31 am
  • Editorial Team
    Editorial Team added an answer OK, here is a fix that we actually used. It… May 12, 2026 at 10:31 am

Related Questions

Could anyone please give me an example in this? I have found many but
I have an untyped XML column in Sql Server Database which holds values such
I have an NSIS based installer that I need to be able generate slightly

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.