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 7506827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:21:40+00:00 2026-05-29T22:21:40+00:00

Here is my code: [TestMethod] public void LoginUnregisteredUserShouldFail() { Mock<IRepository<User>> _repo = new Mock<IRepository<User>>();

  • 0

Here is my code:

    [TestMethod]
    public void LoginUnregisteredUserShouldFail()
    {
        Mock<IRepository<User>> _repo = new Mock<IRepository<User>>();
        UserServiceForTest target = new UserServiceForTest(_repo.Object, new HashingService());

        var unregisteredTestUser = new User() { Email = "a", Nombre = "test", Password = "test" };
        var registeredHashedTestUser = new User() { Email = "test@test.com", Nombre = "test", Password = "qUqP5cyxm6YcTAhz05Hph5gvu9M=" };
        Expression<Func<User, bool>> expression = a => a.Email == "a";
        _repo.Setup(a => a.Single(It.Is<Expression<Func<User,bool>>>(l => l.ToString() == expression.ToString()))).Returns(unregisteredTestUser);
        Assert.IsFalse(target.ValidateCredentials(unregisteredTestUser));
    }

I want to query the Single method of my repo, matching Email, and I want the result to be the specified User.

I dont know what I’m doing wrong but I always receive null.

EDIT:
My implementation is the following:

    private string GetUserPasswordFromDbByUserName(string userName)
    {
        Expression<Func<User, bool>> ax = a => a.Email == userName;
        var axx = ax.ToString();
        var user = _repo.Single(ax);
        if (user != null)
            return user.Password;
        else
            return string.Empty;
    }

It receives a string userName and for some reason, the .ToString() returns ‘a => (a.Email == value(Casita.Services.UserService+<>c__DisplayClass5).userName)’ instead of ‘a => (a.Email == “a”)’. Makes no sense to me, but I’m guessing this is the reason the comparison is failing.

  • 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-29T22:21:40+00:00Added an answer on May 29, 2026 at 10:21 pm

    Your problem is likely the equality comparison in the Is expression parameter. They are probably not the same when converted to strings so your equality comparison might be failing. The following question details how to compare [Func] delegates:

    How to check if two Expression<Func<T, bool>> are the same

    Nievely I’ll suggest the following might work. Note this would use http://evain.net/blog/articles/2008/02/06/an-elegant-linq-to-db4o-provider-and-a-few-linq-tricks which might require adding a reference/download of db4o. I stopped research at that point.

    Func<Expression, Expression, bool> eq =
                    ExpressionEqualityComparer.Instance.Equals;
    
    Expression<Func<User, bool>> expression = a => a.Email == "a";
            _repo.Setup(a => a.Single(It.Is<Expression<Func<User,bool>>>(l => eq(l,expression))).Returns(unregisteredTestUser);
    

    At any rate the core problem is probably that comparison.

    Marc Gravell’s answer seems to imply using the method your using will work, but only if everything including variables in the implementation of your actual method are exactly the same and are passed in that exact same state in the call to the repository dependency.

    My understanding of this is you would have to have exactly the same definition of the expression that you are using for testing and in the actual implementation this is testing.

    I would try debugging and looking at each expression converted to a string (the one in your test method and the one in your implementation, and check if they are exactly the same. If they are then that theory is out the window.)

    It weakens your test, but you could just check if any expression is passed in and return what you want.

    Another alternative that may work for you is checking if the returns of the delegates are the same, but again this is a weaker test:

    _repo.Setup(a => a.Single(It.Is<Expression<Func<User,bool>>>(l = l(unregisteredTestUser)== expression(unregisteredTestUser)))).Returns(unregisteredTestUser);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run the following code with Ninject.Moq: [TestMethod] public void TestMethod1() {
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
The code in question public void StartPlaying() { ThreadPool.QueueUserWorkItem(ignoredState => { while (_playlist.Count >
I try to test my controller [TestMethod] public void Index() { AdminController controller =
How do I unit test a custom ModelBinder? Here's the code. public class MagicBinder
I'm wring a unit test for a controller and here is my code. public
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code from MSDN . I don't understand why the work isn't just
enter code here I have a table on SQL server 2005 with bigint primary
The code here is X++. I know very little about it, though I am

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.