Search code examples
c#rdlc

How to get missing fields to fail?


I have some .rdlc reports. I am trying to write some tests for them, and it was working well. I will get failing tests if the parameters I specified don't match, and for one report I will get an exception if the fields in the report data set don't match the type I'm binding to.

This is great so far, except that the field exceptions are only working in the one report. For the other reports, there are missing fields and they do not generate exceptions; rather, the values just show up empty on the rendered report.

How can I tell at test-time whether the fields in my datatype match the report or not?


Solution

  • After searching through the RDLC code, I determined that during rendering field references will only throw from complex "Visibility" expressions; missing fields will not throw errors from textboxes, including Tablix cells.

    I did discover however that there are warnings that are generated instead. These warnings seem to be sent to debug output. I did not find a way to see them from the LocalReport object itself.

    I was able to generate a meaningful failed test when there are missing fields by intercepting debug output. My report rendering test then looks something like this:

    // create and setup your LocalReport
    
    // intercept debug writing
    using var writer = new StringWriter();
    using var listener = new TextWriterTraceListener(writer);
    Debug.Listeners.Add(listener);
    
    byte[] bytes;
    try {
       bytes = localReport.Render("PDF");
    } finally {
       Debug.Listeners.Remove(listener);
    }
    
    var warnings = writer.ToString();
    if (warnings != "")
       Assert.Fail(warnings);
    
    Assert.IsNotNull(bytes);
    Assert.IsTrue(bytes.Length > 0);
    

    Unfortunately this will still not detect mismatched datatypes. Wrong datatype will result in #Error on the report and does not generate any warning messages.