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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:59:23+00:00 2026-05-13T11:59:23+00:00

I have similar methods in the business layer. I am new to unit testing

  • 0

I have similar methods in the business layer. I am new to unit testing and sometimes get confused. For an idea, can you suggest, what will be a better approach to test this method
behaviour? I am using C# NUnit and Moq

public int? AddNewCatRetID(string categoryName)
{
  int? categoryID = 0;
  Adapter.AddNewBlogCategoryReturnID(categoryName, ref categoryID);

  if (categoryID.HasValue)
    return categoryID;

  return 0;
}

where

Adapter = Visual Studio 2008, Data Set Designer generated TableAdater

AddDeveloperCategoryReturnID() = Name of a function which utilises a Stored procedure in DB

It adds a new record, “Category” and returns its auto generated ID. If it is non zero, we take that result for further processing.

I know should not be interested in talking to Database, below is the procedure, just to give an idea about what is going on in DB

PROCEDURE [dbo].[AddDeveloperCategoryReturnID]

@NAME NVARCHAR(MAX),
@CATEGORY_ID INT OUTPUT
AS
 BEGIN
 INSERT INTO [AllTimeGreatProgrammersDateBase].dbo.CATEGORIES(NAME )
 VALUES (@NAME );

 SET @CATEGORY_ID = SCOPE_IDENTITY();

 SELECT @CATEGORY_ID;
END

some issues

  • how to check the values returned using “ref” from the method
  • what will you prefer to test and not to test? will be great if can list
  • 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-13T11:59:23+00:00Added an answer on May 13, 2026 at 11:59 am

    I’d first convert Adapter.AddNewBlogCategoryReturnID(categoryName, ref categoryID) so that instead of returning a variable by reference, it simply returned the value.

    Then, I would extract that into a virtual method.

    To test AddNewCatRetID, I would extend the class to make a testable version, and override that virtual method to return an int? stored in a public variable.

    That way, when you test to see what happens when you call AddNewCatRetID in a situation where there’s a 0 in the database, you don’t need to actually put a 0 in the database – you just set that parameter on the testable version of your class, and when your test calls AddNewCatRetID, instead if hitting the database, it just returns the value you set. Your test is guaranteed to be faster if you can avoid hitting the database, and since it’s MS’s generated adapter, there’s not really a need to test it – you only care about what your method does with what the adapter returns.

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

Sidebar

Related Questions

I have the following two methods that (as you can see) are similar in
I have a simple marker annotation for methods (similar to the first example in
So I have a really long chain of methods, something similar to: return self.append_command(fbghasjfa).append_command(input_file_part).append_command(output_video_codec_part).append_command(output_resolution_part).append_command(output_video_bitrate_part).append_command(strict_part).append_command(output_audio_codec_part).append_command(output_number_of_audio_channels_part).append_command(output_audio_bitrate_part).append_command(output_file_part).__finalized
I have a test setup where I have many very similar unit tests that
I have some business objects and some very similar corresponding data contract objects for
I have similar methods that needs to be implemented for different concepts. But call
I have several similar methods, say eg. CalculatePoint(...) and CalculateListOfPoints(...). Occasionally, they may not
I have two similar methods that basically does the same thing only with different
I have a constructor method similar to this: public class Foo { public Foo
I have a SafeInvoke Control extension method similar to the one Greg D discusses

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.