0

The parameter in the AssertManager.Record is in action but i need the value inside the lambda action which is the asserts.So i need to get what type of assertion i used which i passed from one class to another. I used lambda expression for certain reason so i cannot edit that. I just need a string type which says whatever type of assertion i did. in my example it should output a "Assert.True" or "Assert.Equal" to the console.

Heres the sample code i use:

public class ClassTest
{
    AssertManager = new AssertManager();

    [Fact]
    public void sampleTestAssert()
    {

      AssertManager.Record(() => Assert.True(true));
      AssertManager.Record(() => Assert.Equal("Dog","Dog"));


    }
}

public class AssertManager
{ 
    public void Record(Action testMethod)
    { 
     //is it possible to use testMethod to get the Assert inside the lambda in the 
     //Output what assert i did (ex. Assert.True, Assert.Equal )
    }
}

please let me know if you have a solution for this. Thank you.

  • Coming at what I think you're trying to do from a different angle, are you able to change the lines of code calling `Assert.True` etc? If so, could you subclass the `Assert` class, and override each method (`True`, `Equals` etc) to record themselves, then call the base class to actually perform the assertion? – James Thorpe Dec 12 '14 at 08:35

1 Answers1

0

You need to use Expression<Action> instead of Action then you can reflect the lambda...

See http://msdn.microsoft.com/en-us/library/bb397951.aspx

Rico Suter
  • 11,548
  • 6
  • 67
  • 93