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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:41:42+00:00 2026-05-12T11:41:42+00:00

Disclaimer: This is my first time writing unit tests…be gentle! :) I am trying

  • 0

Disclaimer: This is my first time writing unit tests…be gentle! 🙂

I am trying to write a test for the following method and would like people’s thoughts an whether I am thinking about this the correct way. I have an interface called IAuthenticationProvider with the following method:

bool Authenticate(string username, string password)

I also have a class called UserAccountService that has the following method:

bool SignIn(string username, string password)

The UserAccountService takes a IAuthenticationProvider interface as part of the constructor and is used within the SignIn method. The SignIn method looks a little like:

public bool SignIn(string username, string password)
{
    // _provider is of type IAuthenticationProvider
    bool result = _provider.Authenticate( username, password );  
    // ....
    return result;
}

Initially when I thought of tests such as “signing in with unknown username”, “signing in with an invalid password”, etc. But, I then began to think that I don’t really want to test the “authentication” aspect, i.e. _provider.Authenticate, but I want to test the actual signing in. So, I thought tests such as “signining in while already signed in”, “signing in when the user cannot be authenticated”, etc, would make more sense.

Would this be the right way of approaching this these type of tests?

Kind Regards

Michael

  • 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-12T11:41:42+00:00Added an answer on May 12, 2026 at 11:41 am

    You can use a mock-based test. This would setup a UserAccountService with a mock implementation of IAuthenticationProvider.

    //happy case provider return true
    mock = createMock(IAuthenticationProvider, true);
    service = new UserAccountService(mock).SignIn(..);
    ...
    
    //provider will return false
    mock = createMock(IAuthenticationProvider, false);
    service = new UserAccountService(mock).SignIn(..);
    ...
    

    You can write your own mock implementations if you like. Most of the time it is easier to use a mock library as easymock:

    //same as above
    mock = createMock(IAuthenticationProvider.class)
    expect(mock.Authenticate(...)).andReturn(true);
    replay(mock)
    service = new UserAccountService(mock).SignIn(..);
    //test here
    

    Mock tests have their own cost and should only be used when a state-based test is not possible. Your application design should be in a way that classes depend on as few classes as possible and can be ideally work on their own (plus your runtime).

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

Sidebar

Related Questions

Disclaimer: i still suck at writing scripts please be gentle if this is a
Disclaimer:It's my first time developing an app that will be on a tablet style
The Disclaimer First of all, I know this question (or close variations) have been
Background / Disclaimer First of all, please feel free to skip this entirely if
Been a while since I've dealt with ASP.NET and this is the first time
Disclaimer: first time I've used DBI. I have a MySQL table with a lot
Disclaimer: the following is a sin against XML. That's why I'm trying to change
Disclaimer: I did try searching first, and even found this comment , but it
Disclaimer: I have no experience in signing the files and this is my first
First off, I need to give a disclaimer: DO NOT RUN THIS JAVASCRIPT It

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.