1

I'm new to the Visual Studio Unit Testing Framework. I've dabbled a little in XUnit, though (DUnit to be specific).

I don't know why the following tests are failing. Based on my C# code (exhibit A), I would think my tests (exhibit B) would pass with the proverbial flying colors.

[EXHIBIT A - Pertinent code]

public class MessageClass
{
    private int _messageTypeCode = 0;
    private int _messageTypeSubcode;
    private int _messageSequenceNumber; 
    private string _messageText;

public MessageClass()
{
    this._messageTypeCode = 0;
    this._messageTypeSubcode = 0;
    this._messageSequenceNumber = 0;
    this._messageText = string.Empty;
}

public void SetMessageTypeSubcode(int AMessageTypeSubcode)
{
    int iMsgTypeSubCode = AMessageTypeSubcode;
    if (iMsgTypeSubCode > 9999)
    {
        iMsgTypeSubCode = 9999;
    }
    else if (iMsgTypeSubCode < 0)
    {
        iMsgTypeSubCode = 42;
    }
    _messageTypeSubcode = AMessageTypeSubcode;
}

public int MessageTypeSubcode()
{
    return _messageTypeSubcode;
}

[EXHIBIT B - Test code in the corresponding MessageClassTest]

[TestMethod()]
public void SetMessageTypeSubcodeTest()
{
    int AMessageTypeSubcode;
    // Should I put this class instantiation in MyTestInitialize?
    MessageClass target = new MessageClass();

    // Test 1
    AMessageTypeSubcode = 0; 
    target.SetMessageTypeSubcode(AMessageTypeSubcode);
    Assert.AreEqual(AMessageTypeSubcode, target.MessageTypeSubcode());

    // Test 2 - 10000 is too much
    AMessageTypeSubcode = 12345;
    target.SetMessageTypeSubcode(AMessageTypeSubcode);
    Assert.AreEqual(9999, target.MessageTypeSubcode());

    // Test 3 - val must be positive
    AMessageTypeSubcode = -77;
    target.SetMessageTypeSubcode(AMessageTypeSubcode);
    Assert.AreEqual(42, target.MessageTypeSubcode());
}

... It is failing on the second test. Having set the val higher than the cutoff (9999), it should be assigned that (9999) rather than 12345.

As I said, I'm new to Visual Studio Unit Testing Framework; is it not possible to have more than one test in a TestMethod? Or do I need to do something like call flush() or finish() or close() or reset() or something?

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

The tests are failing because the test should fail. Your method is incorrect:

_messageTypeSubcode = AMessageTypeSubcode;

Should be:

_messageTypeSubcode = iMsgTypeSubCode ;
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I'm the Mr. Magoo of software development - first I had the code attached to the wrong property (MessageTypeCode instead of MessageTypeSubcode) and I DID fix that error in my misplaced code. – B. Clay Shannon-B. Crow Raven Feb 03 '12 at 20:18