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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:59:47+00:00 2026-06-14T00:59:47+00:00

I asked this question earlier about testing a controller action and verifying that a

  • 0

I asked this question earlier about testing a controller action and verifying that a method in my repository was being called. The answer came back that I should be testing a Save method which is called inside the Register method (both in the same repository) in a seperate test on the repository only. That’s what I thought, but I’m coming to do the test and I can’t get it to work. 🙁

Here’s the repository test, where am I going wrong?

    [TestMethod]
    public void Register_calls_Save_method_when_Member_is_valid()
    {
        _mockMemberRepository.Setup(r => r.GetByEmail(It.IsAny<string>())).Returns((Member)null);            
        MembershipCreateStatus status = _mockMemberRepository.Object.Register(_testMember.Email, "password", "password");
        _mockMemberRepository.Verify(r => r.Save(It.IsAny<Member>()), Times.Once());
    }

Here’s the Register method on the repository:

public MembershipCreateStatus Register(string email, string password, string confirm)
    {
        if (password.Equals(confirm))
        {
            try
            {
                Member m = GetByEmail(email);
                if (m == null)
                {
                    int format = (int)PasswordFormatEnum.Encrypted;
                    string salt = GenerateSalt();
                    string pass = EncodePassword(password, format, salt);

                    m = new Member()
                    {
                        Email = email,
                        Password = pass,
                        PasswordSalt = salt,
                        PasswordFormat = format
                    };
                    Save(m);
                    return MembershipCreateStatus.Success;
                }
                else
                    return MembershipCreateStatus.DuplicateEmail;
                //"A user with that email address already exists. Please use the Forgotten Password link if you need to recover your password.";
            }
            catch (Exception ex)
            {
                _logger.LogError(ex);
                return MembershipCreateStatus.ProviderError;
            }
        }
        return MembershipCreateStatus.InvalidPassword;
    }
  • 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-06-14T00:59:48+00:00Added an answer on June 14, 2026 at 12:59 am

    You can’t use Moq to verify that you’re calling one method on an object from another method on that object. What you can do is verify that something that is called in your Save() method is called.

    For example, if I was writing my own repository that was using Ado.Net to update a database I could do something like the following:

    public class MyRepository : IRepository {
       private readonly IDatabase m_db;
       public MyRepository(IDatabase myDatabase){
          m_db = myDatabase;
       }
    
       public void Register(string email, string password, etc.){
          // ... do stuff ...
          Save(m);
          // ... do stuff ...
       }
    
       public void Save(Member member){
          // ... build sql query ...
          m_db.ExecuteNonQuery(sqlCommand);
    
       }
    }
    

    You’d then pass a mocked database object to your repository in your test and you’d verify that:

    [TestMethod]
    public void Register_calls_Save_method_when_Member_is_valid()
    {
        Mock<IDatabase> _mockDB = new Mock<IDatabase>();
        // Setup mockDB with return values for GetByEmail(), etc.
    
        _repository = new MyRepository(_mockDB.Object);
    
        MembershipCreateStatus status = _repository.Register("Email@Email.com", "password", "password");
        _mockDB.Verify(r => r.ExecuteNonQuery(It.IsAny<SqlCommand>()), Times.Once());
    }
    

    So, you’re not verifying that Save() is called explicitly, but by verifying that the right underlying database call was called you can verify that Save() happened.

    The same approach should work for other frameworks too.

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

Sidebar

Related Questions

This is related to this question I asked earlier about syntax highlighting user-defined blocks
This question relates to this question which I asked earlier this week. The answer
I asked this question earlier about business logic and presentation logic, and it got
I asked this question earlier, and after thinking about the topic some more, I
I've asked about this earlier but the question itself and all the information in
i had asked earlier about the same kind of question but that was in
I earlier asked a question about arrays in scheme (turns out they're called vectors
This is related to an earlier question I asked, where the answer was: If
I asked a question about this earlier and was told to restate the question
I had asked a question about this earlier, but it didn't get answered right

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.