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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:33:42+00:00 2026-06-17T14:33:42+00:00

I have the following class public interface IAuthProvider { string GenerateKey(); } public class

  • 0

I have the following class

public interface IAuthProvider
{
    string GenerateKey();
}

public class AuthProvider : IAuthProvider
{
    public string GenerateKey()
    {
        using (var rng = new RNGCryptoServiceProvider())
        {
            var data = new byte[16];
            rng.GetBytes(data);
            return BitConverter.ToString(data).Replace("-","");
        }
    }
}

I also have the follow unit tests to go with it

[TestClass]
public class AuthProviderTests
{
    private AuthProvider _provider;
    private string _key;

    [TestInitialize]
    public void Initialize()
    {
        _provider = new AuthProvider();
        _key = _provider.GenerateKey();
    }

    [TestMethod]
    public void GenerateKey_key_length_is_32_characters()
    {
        Assert.AreEqual(32, _key.Length);
    }

    [TestMethod]
    public void GenerateKey_key_is_valid_uppercase_hexidecimal_string()
    {
        Assert.IsTrue(_key.All(c =>
            (c >= '0' && c <= '9') || 
            (c >= 'A' && c <= 'F')
        ));
    }

    [TestMethod]
    public void GenerateKey_keys_are_random()
    {
        var keys = new List<string>
            {
                _provider.GenerateKey(),
                _provider.GenerateKey(),
                _provider.GenerateKey(),
                _provider.GenerateKey(),
                _provider.GenerateKey()
            };

        var distinctCount = keys.Distinct().Count();

        Assert.AreEqual(5, distinctCount);
    }
}

Everything works great. However I need to create a method (and tests to go with it) called GenerateSecret. This method will do exactly the same as GenerateKey().

Now I am thinking I should create a method called GenerateRandomHexString(int bytes) and copy the code from GenerateKey into it. Then for GenerateKey and GenerateSecret I should use the follow code:

public interface IAuthProvider
{
    string GenerateKey();
    string GenerateSecret();
    string GenerateRandomHexString(int bytes);
}

public class AuthProvider : IAuthProvider
{
    public string GenerateKey()
    {
        return GenerateRandomHexString(16);
    }

    public string GenerateSecret()
    {
        return GenerateRandomHexString(16);
    }

    public string GenerateRandomHexString(int bytes)
    {
        using (var rng = new RNGCryptoServiceProvider())
        {
            var data = new byte[bytes];
            rng.GetBytes(data);
            return BitConverter.ToString(data).Replace("-","");
        }
    }
}

Now for the tests, should I just write the tests for the GenerateRandomHexString method, or should I write tests also for the GenerateSecret and GenerateKey (which will be pretty much identical tests)

  • 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-17T14:33:43+00:00Added an answer on June 17, 2026 at 2:33 pm

    It’s a bad idea to create many interface methods to do the same thing. I also don’t put overloads on interfaces. The problem this creates is that methods that have the same semantic meaning can have wildly divergent implementations. They might not in the simplest cases, but simple cases often become complex ones eventually.

    I love extension methods for this problem.

    public interface IAuthProvider
    {
        string GenerateKey();
    }
    
    public static class IAuthProviderExtensions
    {
        public static string GenerateSecret(this IAuthProvider provider)
        {
             return provider.GenerateKey();
        }
    }
    

    Test:

    [Test]
    public void GenerateSecretIsAliasForGenerateKey()
    {
        var mockProvider = new Mock<IAuthProvider>();
        var key = GenerateARandomStringSomehow();
        mockProvider.Setup(p=>p.GenerateKey()).Returns(key);
        Assert.That(mockProvider.Object.GenerateSecret(), Is.EqualTo(key));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
I have the following template method declared in my interface: class IObjectFactory { public:
I have the following class public class MenuVeiculo { public string Nome { get;
I have the following class public class UIControl { public string FName{ get; set;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
We have the following class hierarchy: public interface IManager { object GetObject(int); } public
I have following code public interface IDummy<TType> where TType : new() { } public
I have the following interface: public interface ILogger { void Debug(string message, params object[]
Have following code: public interface ITest { string St1 { get; } } public

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.