Debugging business logic in AX 2009 SSRS reports

本文介绍如何使用Visual Studio进行AX2009 SSRS报告的业务逻辑调试,通过创建单元测试来验证业务逻辑的正确性,并提供代码覆盖率分析,以缩短调试周期并遵循测试驱动开发原则。

Debugging business logic in AX 2009 SSRS reports

Published 30 August 08 02:43 PM | vnicolas 

Background

With AX2009 you are now able to do SSRS reports in Visual Studio. The typical process for designing a report consist in creating a query and bind to it, code some business logic (BL) specific to the report and design the visual part.

This post will focus on debugging the report business logic. For more information on designing reports in general, see the msdn documentation

There is also an excellent post on Channel 9 from my friend Manoj here.

Debugging

Let say you've designed a fancy report in VS and in order to make it do cool stuff, you had to write some pretty complex business logic. Unfortunately, your business logic does not do what you think it should be doing. You know that you've been working too late on too much caffeine, so you suspect that there might be a bug there and you need to do some debugging.

The 'attach-to-the-server-process' approach

One approach to debug your business logic consist of the following steps

· Rebuild the report in VS in debug mode

· Save it back to the AOD

· Redeploy the report

· Set a breakpoint at a strategic location in your business logic code

· Attach to the server process

· Invoke your report

· Hit the breakpoint and start debugging

While being perfectly valid, this approach has a rather long turn-around-time, especially considering the fact that during a debugging session, you will most likely need to ride the modify-code-rebuild-test carrousel several times.

Also you are an agile programmer and you know about the values of Test Driven Development (TDD), so you are thinking: 'Damn, I wish I had developed this report using the good TDD methods and wrote some unit test for my business logic'. Well, don't despair, Visual Studio (professional edition and above) comes with several cool features that can help you do TDD and debugging on your reports. Let's see how.

Creating unit test for SSRS based reports business logic

Let's assume that you have created your report solution with its related report project

clip_image002

For the sake of simplicity, let's pretend that you have written the following dull BL method

public static DataTable MyMethod()

        DataTable result = new DataTable(); 
        result.Columns.Add("Col1"); 
        result.Columns.Add("Col2"); 
       result.Columns.Add("Col3"); 
        result.Columns.Add("Col4"); 

        return result;
}

Now we are going to create a unit test for this method. But before doing so, we need to help Visual Studio a bit to resolve certain assemblies. In the business logic project, go the reference node, select the Microsoft.Dynamics.Framework.Reports assembly and set the property 'Copy Local' to 'True'.

Then, right-click in the editor and choose 'Create unit test...'. Browse to the folder where the business logic assembly is built (for example <...>/DynamicsReportsLibrary1/DynamicsReportsLibrary1/BusinessLogic/bin/debug/)

and select the DynamicsReportsLibrary1.BusinessLogic.dll assembly. Unfold the tree node and select the method you want to debug as shown below:

clip_image004

You'll notice that Visual Studio will create test project for you if you don't have one. Alternatively, you can create one by right clicking on the solution and choosing 'Add project' and then select the test project template:

clip_image006

Then you can target this project when creating a unit test.

Visual Studio has generated the following unit test method :

[TestMethod()]
public void MyMethodTest()
{
      DataTable expected = null; // TODO: Initialize to an appropriate value
      DataTable actual;
      actual = Report1.MyMethod();
      Assert.AreEqual(expected, actual);
      Assert.Inconclusive("Verify the correctness of this test method.");
} In our example, as we use the DataTable type, we'll also need to add references to System.Data and System.xml in order to get your test project to build. Once you've done that, you should be able to build your solution and run the test. You can either do this from the test toolbar:

 

clip_image008

or from the test view, where you can select each unit test individually.

clip_image010

Now you can start rewriting the auto-generated test and do some real testing. For the sake of simplicity, I'll write a simple test, which checks that the created data table actually has 4 columns.

public void MyMethodTest()
{
      DataTable dataTable = Report1.MyMethod();
      Assert.AreEqual(dataTable.Columns.Count, 4, "The data table must have 4 colums");
}

Running this test provides the following output:

clip_image012

What about Debugging?

That's what we initially wanted to achieve, wasn't it?. Well, that's just a few clicks away. Put a breakpoint at the beginning of your BL method and from the test view window, right-click on the test method and select 'Debug Selection'.

clip_image014

Next thing you know, you are hitting your breakpoint and you can start, debugging your business logic:

clip_image016

Code coverage

As a freebie, you can get Visual Studio to calculate the code coverage for you. Simply edit the test configuration (Tools -> Edit Test Run Configuration -> Local Test Run) to enable the code coverage instrumentation on your BL assembly

clip_image018

Then re-run the test, and click on the 'Show Code Coverage Results' icon in the toolbar of the Test Results window, which will give you the nice view below for free:

clip_image020

Conclusion

The technique described above shows how to debug business logic for SSRS based AX 2009 reports with a quick turn-around time, following the Test Driven Development methodology and providing code coverage measurement on the side. Beware that there might be some scenarios where attaching to the server process is the only way to track a specific bug. Some bugs might only show there ugly faces when the report in running in its deployment context on the server. But if you follow the good principles of TDD and use this local debugging approach, you might not need to go to these extends and you'll likely catch most of the errors at an early stage. Happy debugging :-).

提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值