格式化 Fitnesse RowFixture 中的数据

发布于 2024-07-05 20:44:31 字数 237 浏览 7 评论 0原文

我有一个 Fitnesse RowFixture,它返回业务对象列表。 该对象有一个字段,它是一个浮点数,表示 0 到 1 之间的百分比。业务对象的消费者将是来自设计者的网页或报表,因此百分比的格式将由设计师而不是业务对象决定。

如果页面能够模仿设计者将数字转换为百分比,即不显示 0.5,而是显示 50%,那就更好了。 但我不想用显示代码污染业务对象。 有没有办法在 RowFixture 中指定格式字符串?

I've got a Fitnesse RowFixture that returns a list of business objects. 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.

It would be nicer if the page could emulate the designer when converting the number to a percentage, i.e. instead of displaying 0.5, it should display 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-07-12 20:44:31

您当然不想仅仅为了让测试看起来更好而修改业务逻辑。 不过,好消息是,有一种方法可以完成此任务,该方法并不困难,但不像传递格式说明符那么容易。

尝试将您的 Fit Fixture 视为 FitNesse 和您的应用程序代码之间的服务边界。 您想要定义一个不一定需要更改的合同,如果您的SUTS系统测试)改变。

让我们看一下业务对象的简化版本:

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

由于 RowFixture 的工作方式,我们需要定义一个将作为契约工作的简单对象。 通常我们会使用一个接口,但这并不能满足我们的目的,因此一个简单的DTODataT传输 >Oobject) 就足够了。

像这样的事情:

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

现在我们可以定义一个 RowFixture,它将返回我们的自定义 DTO 对象的列表。 我们还需要创建一种将 BusinessObject 转换为 ReturnRowDTO 的方法。 我们最终得到了一个看起来像这样的夹具。

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("%")};
        }
    }

现在,您可以更改基础 BusinessObjects,而不会破坏实际的适合度测试。 希望这可以帮助。

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. You want to define a contract that doesn't necessarily have to change if the implementation details of your SUT (System Under Test) change.

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. Ordinarily we would use an interface, but that isn't going to serve our purpose here so a simple DTO (Data Transfer Object) will suffice.

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. We also need to create a way to convert BusinessObjects to ReturnRowDTOs. We end up with a Fixture that looks something like this.

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. Hope this helps.

秋叶绚丽 2024-07-12 20:44:31

我不确定“污染”是什么。 要么要求您的业务对象返回一个以百分比表示的值,在这种情况下,您的业务对象应该提供该值,要么您正在测试您现在拥有的浮动响应的真实值。

试图让 Fitnesse 来提高可读性的价值似乎有点奇怪。

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.

Trying to get fitnesse to massage the value for readability seems a bit odd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文