Format data in Fie RowFixture

I have a Fitnesse RowFixture that returns a list of business objects. The object has a field, which is a float representing a percentage from 0 to 1. The consumer of the business object will be a web page or report that comes from the designer, so formatting the percentage will correspond to the design, not the business object.

It would be better if the page could emulate the constructor when converting a number to a percentage, i.e. instead of displaying 0.5, it should be displayed at 50%. But I would prefer not to pollute the business object with the displayed code. Is there a way to specify a format string in a RowFixture?

+6
fitnesse
source share
2 answers

Of course, you don’t want to change your business logic to make your tests look better. The good news, however, is there is a way to do this, which is not difficult, but not as simple as passing 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 does not have to be changed if the details of the implementation of your SUT ( S ystem Under T est).

Let's look at a simplified version of your business object:

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

Due to the fact that RowFixture works, we need to define a simple object that will work as a contract. Usually we used the interface, but this will not serve our purpose here, therefore a simple DTO ( D ata T ransfer O bject).

Something like that:

 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 ReturnRowDTO. In the end, we have 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("%")}; } } 

Now you can modify the underlying BusinessObjects without breaking the actual Fit Tests. Hope this helps.

+3
source share

I’m not sure what β€œpolitics” is. Any requirement is that your business object returns a value expressed as a percentage, in which case your business object should offer this -OR-, you check the true value of the response as the float that you have now.

Trying to get a massage fit for readability seems a little odd.

0
source share

All Articles