How to ensure Debug.Assert works properly using NUnit

I tried to work around this problem: How to create a unit test that checks to see if the function terminates because Debug.Assert (from System.Diagnostics) fails, marking it as passed when it does it and as unsuccessful if it isn't.

I know it NUnithas a function [ExpectedException(typeof( ArgumentException ) )], but I cannot figure out what this exception is from the MSDN website. Intuition will say that it can be something like an AssertionException, and that it exists ... but is part of the NUnit framework. I assume that the NUnit exception claim that it throws. I could just destroy it using:

[ExpectedException(typeof(Exception))]

But this gives rise to the problem of the appearance of a standard debug window. In my search, I stumbled on removing this window due to the appearance at all, but it is like taking the surgical table, which usually uses a scalpel. Because I want to see this window when something unexpected happens, when I execute my program.

I assume that there is a way to replace the method Debug.Assertwith an NUnit-component (I am still early in my project, so it is not too big for refactoring), but I assume that many programmers stick to the functionality Debug.Assertas standard in .NET.

As such, I would like to know how to “claim” Debug.Assertionfailures, not “kill” the Windows debugging screen from my project?

, . , , To-Wound Warhammer 40K , .

static public int assaultToHit(int _attacker_WeaponSkill,
            int _defender_WeaponSkill)
        {
            //Preconditions
            Debug.Assert(_attacker_WeaponSkill >= 1 && _attacker_WeaponSkill <= 10,
                "Weapon Skill stat must be in range [1,10]");
            Debug.Assert(_defender_WeaponSkill >= 1 && _defender_WeaponSkill <= 10,
                "Weapon Skill stat must be in range [1,10]");

            int target;
            if (_attacker_WeaponSkill > _defender_WeaponSkill)
            {
                target=3;
            }
            else if (_defender_WeaponSkill >= (_attacker_WeaponSkill + _attacker_WeaponSkill + 1))
            {
                target=5;
            }
            else
            {
                target=4;
            }

            //postconditions
            Debug.Assert(target >= 3 && target <= 5,
                "To hit target for assault must be in range [3,5]");

            return target;
        }

:

    [TestCase(-1,2)]
    [TestCase(1, -2)]
    [TestCase(-1, -2)]
    [TestCase(11, 2)]
    [TestCase(1, 20)]
    [TestCase(11, 20)] 
    [ExpectedException(typeof(Exception))]
    public void testContract_AssaultToHit(int _attacker_weaponskill, 
        int _defender_weaponskill)
    {
        Warhammer40kRules.assaultToHit(_attacker_weaponskill, 
            _defender_weaponskill);
    }
+5
3

fooobar.com/questions/4110/... , . - , , , .

, :

static public int assaultToHit(int _attacker_WeaponSkill,
        int _defender_WeaponSkill)
    {
        //Preconditions
        if(!(_attacker_WeaponSkill >= 1 && _attacker_WeaponSkill <= 10))
        {
            throw new ArgumentOutOfRangeException("Attackers WeaponSkill must be in range [1,10]");
        }
        if(!(_defender_WeaponSkill >= 1 && _defender_WeaponSkill <= 10))
        {
            throw new ArgumentOutOfRangeException("Defenders WeaponSkill must be in range [1,10]");
        }

        ...
        //rest unchanged
    }

NUnit:

[TestCase(-1,2)]
[TestCase(1, -2)]
[TestCase(-1, -2)]
[TestCase(11, 2)]
[TestCase(1, 20)]
[TestCase(11, 20)] 
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void testContract_AssaultToHit(int _attacker_weaponskill, 
    int _defender_weaponskill)
{
    Warhammer40kRules.assaultToHit(_attacker_weaponskill, 
        _defender_weaponskill);
}

[12 2014 ]: , Design by Contract. DbC, : " , . , undefined"., " w91 > " , - . , , , , , (... ;)). , undefined.

, , , , , Exception. .

+2

, . Debug.Assert() .

Unit Test , , , /etc Debug.Assert(). , .

MSDN , Assert.

:

+5

Microsoft " " , , , .

, , , .

Debug.Asserts - , , , , , , null , .

+1
source

All Articles