locked
How to test lambda Expression RRS feed

  • Question

  • I have a property in which I log using Common.Logging.

    log object is a private readonly to class

     log.Info(m => m("Log Status {0} : ", DateTime.Now));
    

    I am not able to cover it in unit test. However if I use 

     
    log.Info("Log Status {0} : ", DateTime.Now));

    It is covered. But i have to use first option. Any idea how to cover it


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Tuesday, November 27, 2012 6:10 AM

Answers

  • It got resolved IsdebugEnabled is set to false hence it wasn't running through lambda expression.


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    • Marked as answer by Amanda Zhu Friday, November 30, 2012 6:58 AM
    Friday, November 30, 2012 5:17 AM

All replies

  • Hello RohitArora,

    Thank you for posting in the MSDN forum.

    I am not able to cover it in unit test.

    Do you mean that the line was not covered in Code Coverage result? Is this line in unit test method? What do you want to test about the lambda expression?

    I am not sure about your scenario. If possible, you can provide an example of your unit test so that we can further look at this issue.

    If you get some errors after unit test run, you also can provide detailed error message.

    Generally we can use unit test to check if a method has logical error. I think that you can wrap your code in a method of a class and then unit test this method. And you can use Assert to compare the expected result and actual result.

    In addition, I did some research and find some articles about unit testing lambda expression that you can refer to:

    http://journeymanyears.wordpress.com/2011/12/10/unit-testing-lambda-expressions/

    http://apparch.wordpress.com/2012/02/13/unit-testing-moq-and-lambda-expression-in-methods/

    http://stackoverflow.com/questions/2491987/how-do-i-unit-test-a-c-sharp-function-which-returns-a-funcsomething

    I hope it can help you.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, November 27, 2012 9:14 AM
  • Amanda,

           Thanks for reply,

    yes this line remains uncovered in CodeCoverage results. How I can cover this in code coverage result.


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Tuesday, November 27, 2012 9:21 AM
  • Hello RohitArora,

    As I said in my previous reply, can you provide us more information?

    Meanwhile you can check the articles above to see if they can help you.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, November 27, 2012 9:25 AM
  •  public interface ISomeService
     {
          StatusReport status {get;}
     }
     public  class StatusReport
     {
     public string version;
     public int count;
     }
     public class DoService : ISomeService
     {
    private readonly ILog log = LogManager.GetLogger<DoService>();
    public StatusReport StatusReport
    {
    get
    {
    var status = new StatusReport
    {
    count = 255,
    version = "1.0"
    }
    log.Info(m => m("Log Status {0} : ", DateTime.Now));
    return status;
    }
    }
     }

    In this above code wrote test as below

     

    public void UnitTest()
     {
    // Arrange
    var  DoService = new DoService();

    // Act
    var statusRx = DoService.StatusReport;

    // Assert
    Assert.IsNotNull(statusRx);
    }

    When I check code coverage it shows the portion mentioned above as uncovered.


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Tuesday, November 27, 2012 9:42 AM
  • Hello RohitArora,

    Glad to receive your reply.

    I have tried to repro your scenario, but I work with the ILog and LogManager in a C# class with errors. I am not an expert of development. It is hard for us to repro this issue, maybe you could share us your project, then we try to run it in our PC. Please attach your Visual Studio project and test project, you can upload them to the sky driver, and then share the download link in your post. Thanks for your understanding.

    In addition, generally if the line was not covered in the Code Coverage result, it means that it is not executed during run. I suggest that you can check the logic of your program to make sure the line can be reached and executed during run.

    Meanwhile, I would like to know if you get some errors at the line when you compile the code and after run. Since the line is not executed, can you get the expected test result? Whether the lines above and below the log.info statement are executed?

    If possible you can provide more information so that we can further look at this issue. I will try my best to help you.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, November 28, 2012 5:38 AM
  • Hello RohitArora,

    What about your issue now? Could you get useful information from our reply?

    Would you mind letting us know the result of the suggestion?

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 29, 2012 8:39 AM
  • It got resolved IsdebugEnabled is set to false hence it wasn't running through lambda expression.


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    • Marked as answer by Amanda Zhu Friday, November 30, 2012 6:58 AM
    Friday, November 30, 2012 5:17 AM
  • Hello RohitArora,

    Glad to see this issue has been resolved and thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, November 30, 2012 7:00 AM