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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:25:03+00:00 2026-05-24T08:25:03+00:00

I have a method like: public virtual IList<string> Validate() { … } I want

  • 0

I have a method like:

public virtual IList<string> Validate()
{
   ...
}

I want to unit test this using NUnit. This is part of a class Vehicle.

A Vehicle can be of different types ie. Car Boat Truck etc.

At the top of my TestFixture I set up the VehicleTypes:

private VehicleType[] _vehicleTypes;

[SetUp]
public void MyTestInitialize()
{
    transScope = new TransactionScope();

    var boat= new VehicleType { Name = "boat" };
    var car = new VehicleType { Name = "car" };
    var truck = new VehicleType { Name = "truck" };

    _vehicleTypes= new VehicleType[] { boat, car, truck };

    ...
}

What I want is to test that an error message is sent back from the method for the boat only.

My unit test is as follows:

[TestCase(0, "This vehicle is inappropriate because it doesn't have wheels")]
[TestCase(1, null)]
[TestCase(2, null)]
public void Validate_Vehicle_ReturnsAppropriateErrorMessage(int vehicleType, string expectedResult)
{
   var vehicle = new Vehicle { VehicleType = _vehicleTypes[vehicleType] };

   var results = vehicle.Validate();

   string result;

   if (results.Count == 0)
      result = null;
   else
      result = results[0];

   Assert.IsTrue(expectedResult == result);
}

So this was how I was trying to test it using TestCases. However I’m not sure this is the right approach as unit tests shouldn’t contain ifs?

Also maybe this is a weird approach to writing a test for different types?

Anyone have any better suggestions?

  • 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-24T08:25:04+00:00Added an answer on May 24, 2026 at 8:25 am

    I would break these up into multiple tests. By doing so you can write one that test the normal behavior (non boat) as well as the boat. If any of these test fail in the future, you won’t have to try and figure out what iteration of the data driven tests failed. The test will speak for itself.

    In this case I would write one for the behavior for the boat and one for the non boat. The other iterations aren’t interesting (and likly use the same code path this test is validating as the other non boats)

    public void Validate_VehicleIsBoat_ReturnsAppropriateErrorMessage()
    {   
       string expectedResult = "This vehicle is inappropriate because it doesn't have wheels";
       var vehicle = new Vehicle { VehicleType = VehicleType.Boat };  //or whatever it is in your enum
    
       var results = vehicle.Validate();   
    
       Assert.AreEqual( expectedResult, results[0] );
    }
    
    public void Validate_VehicleIsNotBoat_DoesNotReturnErrorMessage()
    {   
       var vehicle = new Vehicle { VehicleType = VehicleType.Car };  //or whatever it is in your enum
    
       var results = vehicle.Validate();   
    
       Assert.IsNull( results ); // or whatever the no error message case is. Will results[0] have an empty string?
    }
    

    You could add additional tests to validate the result sets have all the data you want as well.

    Anyway, hope this helps

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

Sidebar

Related Questions

Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Let's say we have a method signature like public static function explodeDn($dn, array &$keys
I have a class that contains two methods like these: public String getFoo(Int32 a)
I would like to convert a string into a node. I have a method
I have a C++ method signature that looks like this: static extern void ImageProcessing(
I have a class like so: public class ClassA { public bool MethodA() {
If you have a controller method like so: @expose(json) def artists(self, action=view,artist_id=None): artists=session.query(model.Artist).all() return
I'd like to be able to call getProgram on objects which have that method,
So, I have a method that performs a parametrised LIKE query. The method takes
I have a vararg method that I'd like to act as a proxy for

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.