简体   繁体   English

在Fitnesse RowFixture中格式化数据

[英]Formatting data in a Fitnesse RowFixture

I've got a Fitnesse RowFixture that returns a list of business objects. 我有一个返回业务对象列表的Fitnesse RowFixture。 The object has a field which is a float representing a percentage between 0 and 1. The consumer of the business object will be a web page or report that comes from a designer, so the formatting of the percentage will be up to the designer rather than the business object. 该对象有一个字段,它表示一个介于0和1之间的百分比。业务对象的使用者将是来自设计者的网页或报告,因此百分比的格式将取决于设计者而不是业务对象。

It would be nicer if the page could emulate the designer when converting the number to a percentage, ie instead of displaying 0.5, it should display 50%. 如果页面在将数字转换为百分比时模拟设计器会更好,即不显示0.5,它应该显示50%。 But I'd rather not pollute the business object with the display code. 但我宁愿不用显示代码污染业务对象。 Is there a way to specify a format string in the RowFixture? 有没有办法在RowFixture中指定格式字符串?

You certainly don't want to modify your Business Logic just to make your tests look better. 您当然不希望修改您的业务逻辑只是为了让您的测试看起来更好。 Good news however, there is a way to accomplish this that is not difficult, but not as easy as passing in a format specifier. 然而,好消息是,有一种方法可以实现这一点并不困难,但并不像传递格式说明符那么容易。

Try to think of your Fit Fixture as a service boundary between FitNesse and your application code. 尝试将Fit Fixture视为FitNesse与应用程序代码之间的服务边界。 You want to define a contract that doesn't necessarily have to change if the implementation details of your SUT ( S ystem U nder T est) change. 要定义一个合同,这并不一定需要改变,如果你的SUT的实施细则( 体系ü的nDer 牛逼美国东部时间)的变化。

Lets look at a simplified version of your Business Object: 让我们看一下您的业务对象的简化版本:

public class BusinessObject
{
    public float Percent { get; private set; }
}

Becuase of the way that a RowFixture works we need to define a simple object that will work as the contract. 由于RowFixture的工作方式,我们需要定义一个可以作为契约的简单对象。 Ordinarily we would use an interface, but that isn't going to serve our purpose here so a simple DTO ( D ata T ransfer O bject) will suffice. 通常我们会使用一个接口,但这不符合我们的目的,所以一个简单的DTOD ata T ransfer O bject)就足够了。

Something Like This: 像这样的东西:

public class ReturnRowDTO
{
    public String Percent { get; set; }
}

Now we can define a RowFixture that will return a list of our custom DTO objects. 现在我们可以定义一个RowFixture,它将返回我们的自定义DTO对象列表。 We also need to create a way to convert BusinessObjects to ReturnRowDTOs. 我们还需要创建一种将BusinessObjects转换为ReturnRowDTO的方法。 We end up with a Fixture that looks something like this. 我们最终得到了一个类似于此的Fixture。

public class ExampleRowFixture: fit.RowFixture
    {
        private ISomeService _someService;

        public override object[] Query()
        {
            BusinessObject[] list = _someService.GetBusinessObjects();

            return Array.ConvertAll(list, new Converter<BusinessObject, ReturnRowDTO>(ConvertBusinessObjectToDTO));
        }

        public override Type GetTargetClass()
        {
            return typeof (ReturnRowDTO);
        }

        public ReturnRowDTO ConvertBusinessObjectToDTO(BusinessObject businessObject)
        {
            return new ReturnRowDTO() {Percent = businessObject.Percent.ToString("%")};
        }
    }

You can now change your underlying BusinessObjects around without breaking your actual Fit Tests. 您现在可以在不破坏实际适合度测试的情况下更改基础BusinessObjects。 Hope this helps. 希望这可以帮助。

I'm not sure what the "polution" is. 我不确定“污染”是什么。 Either the requirement is that your Business Object returns a value expressed as a percentage, in which case your business object should offer that -OR- you are testing the true value of the response as float, which you have now. 要么是您的业务对象返回一个以百分比表示的值,在这种情况下,您的业务对象应该提供 - 或 - 您正在测试响应的真实值为float,您现在拥有。

Trying to get fitnesse to massage the value for readability seems a bit odd. 试图让fitnesse按摩价值以提高可读性似乎有点奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM