4

Starting to implement unit tests in C#, using MSTest

In this particular test I am trying to verify that an ArgumentNullException is being thrown. Even though my code does throw the exception my test is failing because it apparently did not receive that type of exception.

Where am I going wrong? Bound to be something simple....

My test looks like this:

[TestMethod()]
[ExpectedException(typeof(ArgumentNullException), "A null HttpContent was inappropriately allowed")]
public void Test_HttpContent_Null_Throws_Exception()
{
    MultipartFormDataMemoryStreamProvider provider = new MultipartFormDataMemoryStreamProvider();
    Assert.ThrowsException<ArgumentNullException>(()=>provider.GetStream(null, null));
}

And the GetStreams() method looks like this:

public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
    {
        if (parent == null)
            throw new ArgumentNullException("parent");

        if (headers == null)
            throw new ArgumentNullException("headers");

        var contentDisposition = headers.ContentDisposition;
        if (contentDisposition == null)
            throw new InvalidOperationException("Did not find required 'Content-Disposition' header field in MIME multipart body part.");

        _isFormData.Add(String.IsNullOrEmpty(contentDisposition.FileName));
        return base.GetStream(parent, headers);
    }
Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
  • Just curious, seems everyone uses either xUnit or NUnit these days. What motivated you to choose MSTest? I'm not trying to start a debate or flamewar, but I was surprised anyone would choose it in 2017 – Novaterata May 18 '17 at 12:56
  • It's a fair question. Ultimately it ships with Visual Studio and is the .Net default. I did a little Googling beforehand and couldn't find anything definitive that wasn't biased in its writing. In a nutshell, which do you prefer and why? I am still early in these tests and can switch them out easily, before we start implementing company-wide. Also, we are going to be writing integration tests too - think i read something somewhere that MSTest handles these better (can't remember where i read it) – Darren Wainwright May 18 '17 at 13:01
  • I've only used NUnit 2 and 3, NUnit 2 was a pain for parallelization, I had to roll my own, but NUnit 3 solved this. For me the main thing is the support for parameterized tests and test suites (TestFixtures). It was my understanding that MSTest doesn't support parameterized tests, but maybe that changed in V2. xUnit seemed to be the new gold standard for unit tests. It's used by .NET Core and MSBuild as MS Test isn't cross platform. I don't have a strong opinion, but you might be picking the WinForms of test frameworks and wanted to warn you. – Novaterata May 18 '17 at 13:35
  • Basically, I can't explain right now as I'm writing in Java right now, but if I was required to use MSTest by my team I would be very annoyed. LIke being forced to use c# 2.0 – Novaterata May 18 '17 at 13:37
  • Cool, thanks. Trying to get nunit to play with VS2017 at the mo; not going well... – Darren Wainwright May 18 '17 at 13:42
  • http://stackoverflow.com/questions/43007761/how-to-run-nunit-tests-in-visual-studio-2017 – Novaterata May 18 '17 at 13:47
  • Thanks. I've added those though can't get live-testing to work. Thanks though. – Darren Wainwright May 18 '17 at 13:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144582/discussion-between-novaterata-and-darren). – Novaterata May 18 '17 at 15:23
  • 1
    Thanks, though I just got it all working so kicking the tires on NUnit now :D – Darren Wainwright May 18 '17 at 15:26

1 Answers1

5

The assert in this line is handling the exception:

Assert.ThrowsException<ArgumentNullException>(()=>provider.GetStream(null, null));

So the testing framework isn't seeing it being thrown as far as ExpectedException is concerned. You can either remove the attribute, or the assert:

[TestMethod()]
[ExpectedException(typeof(ArgumentNullException), "A null HttpContent was inappropriately allowed")]
public void Test_HttpContent_Null_Throws_Exception()
{
    MultipartFormDataMemoryStreamProvider provider = new MultipartFormDataMemoryStreamProvider();
    provider.GetStream(null, null);
}
James Thorpe
  • 31,411
  • 5
  • 72
  • 93