Skip to main content
Removed meta information (this belongs in comments).
Source Link
Peter Mortensen
  • 31.1k
  • 22
  • 111
  • 134

This is an old yet relevant question with outdated answers so I'm adding the currentA solution that actually works:

public void Test() {
    throw new MyCustomException("You can't do that!");
}

[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
    var exception = Assert.ThrowsException<MyCustomException>(
        () => Test(),
        "This should have thrown!");
    Assert.AreEqual("You can't do that!", exception.Message);
}

This works with using Microsoft.VisualStudio.TestTools.UnitTesting;.

This is an old yet relevant question with outdated answers so I'm adding the current solution:

public void Test() {
    throw new MyCustomException("You can't do that!");
}

[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
    var exception = Assert.ThrowsException<MyCustomException>(
        () => Test(),
        "This should have thrown!");
    Assert.AreEqual("You can't do that!", exception.Message);
}

This works with using Microsoft.VisualStudio.TestTools.UnitTesting;

A solution that actually works:

public void Test() {
    throw new MyCustomException("You can't do that!");
}

[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
    var exception = Assert.ThrowsException<MyCustomException>(
        () => Test(),
        "This should have thrown!");
    Assert.AreEqual("You can't do that!", exception.Message);
}

This works with using Microsoft.VisualStudio.TestTools.UnitTesting;.

my answer was incorrect; fixed
Source Link
Tvde1
  • 1.3k
  • 3
  • 21
  • 44

This is an old yet relevant question with outdated answers so I'm adding the current solution:

public void Test() {
    throw new MyCustomException("You can't do that!");
}

[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
    var exception = Assert.ThrowsException<MyCustomException>(
        () => Test(),
        "This should have thrown!");
    Assert.AreEqual("You can't do that!", exception.Message);
}

This works with using Microsoft.VisualStudio.TestTools.UnitTesting;

This is an old yet relevant question with outdated answers so I'm adding the current solution:

public void Test() {
    throw new MyCustomException("You can't do that!");
}

[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
    Assert.ThrowsException<MyCustomException>(
        () => Test(),
        "You can't do that!");
}

This works with using Microsoft.VisualStudio.TestTools.UnitTesting;

This is an old yet relevant question with outdated answers so I'm adding the current solution:

public void Test() {
    throw new MyCustomException("You can't do that!");
}

[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
    var exception = Assert.ThrowsException<MyCustomException>(
        () => Test(),
        "This should have thrown!");
    Assert.AreEqual("You can't do that!", exception.Message);
}

This works with using Microsoft.VisualStudio.TestTools.UnitTesting;

Source Link
Tvde1
  • 1.3k
  • 3
  • 21
  • 44

This is an old yet relevant question with outdated answers so I'm adding the current solution:

public void Test() {
    throw new MyCustomException("You can't do that!");
}

[TestMethod]
public void ThisWillPassIfExceptionThrown()
{
    Assert.ThrowsException<MyCustomException>(
        () => Test(),
        "You can't do that!");
}

This works with using Microsoft.VisualStudio.TestTools.UnitTesting;