Dnguy025
Dnguy025

Reputation: 91

C# unit test currency validation

I need to write a unit test to test if the input is a valid currency value. I have tried several ways, but i don't think it's the correct way to test for currency value.

TEST CASE:

namespace validationUnitTest
{
    // Validation rule for checking if the input is a valid currency value

    public class CurrencyValidationRule: ValidationRule;
    {
        private static ValidationRule validationRule;

        /// Singleton instance of this <see cref="ValidationRule"/>

        public static ValidationRule Instance { get { return validationRule ?? (validationRule = new CurrencyValidationRule());  } }

        // performs validation checks on a value

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value is double) return ValidationResult.ValidResult;
            string stringValue = value as string;
            double result;
            return string.IsNullOrEmpty(stringValue) || double.TryParse(stringValue, NumberStyles.Currency, cultureInfo, out result) ? ValidationResult.ValidResult : new ValidationResult(false, "Value must be an number");
        }
    }

}

UNIT TEST:

namespace validationUnitTest.Tests
{
[TestClass()]
public class CurrencyValidationRuleTests
{
    [TestMethod()]
    public void ValidateTest()
    {
        var test = new CurrencyValidationRule();

        var expected = new ValidationResult(true, true);

        var actual = test.Validate("0", null);

        Assert.AreEqual(expected.ErrorContent, actual.ErrorContent);
        Assert.AreEqual(expected.IsValid, actual.Isvalid);
        Assert.AreEqual(expected.GetHashCode(), actual.GetHashCode());
    }
}

}

ANY idea on how I should start my unit test, or what I should be search for? I need some starting points. Any thing is greatly appreciate.

Upvotes: 1

Views: 1190

Answers (2)

yash fale
yash fale

Reputation: 331

using System;
using System.Linq;
using Xunit;
using MonthlyStatements.Controllers;
using MonthlyStatements.Models;
using System.IO;
using System.Web.Mvc;

namespace UnitTestProject1
{
    public class UnitTest1
    {
        [Theory]
        [InlineData(10.20, "System.Double")]
        [InlineData(0, "System.Double")]
        public void checkValidation(double amt, string ExpectedResult)
        {
            //Arrange
            string actualResult = "";
            //Act
            //double doubleAmt = double.Parse(amt.ToString());
            actualResult = amt.GetType().ToString();
            //Assert
             Assert.Equal(ExpectedResult, actualResult);
        }
    }
}

Upvotes: 1

yash fale
yash fale

Reputation: 331

use Xunit instead of nUnit , before start developing we have to conside following

  1. Rules : 1) we test only public methods 2) we have to know input parameter and out Put parameter like expected Result and Actual result

in above code you used 3 assert it is not correct way, here you can check data Type of actual result, use xunit [InlineData()] so dont need to repeat code .

Upvotes: 0

Related Questions