jeudi 27 août 2015

Serialize to JSON with lower case property values

I have a operations layer that gets an Entity Framework database context injected into it. I am writing unit tests against the operations layer by mocking the database context using Moq as described here. The mocked data is stored in text files in JSON format, which are created from data in a real database since the data is a bit too complex to make up from scratch.

Everything's going well except for one little issue. When testing some query functions, I'm having some trouble with case sensitivity. When the application runs, it translates the LINQ queries into SQL; since like %search value% is case insensitive, the case of the search term doesn't matter. But since the mocked data is being loaded from JSON and is not going through SQL, tests are failing when they shouldn't.

For example, I have two mocked records, one has a field value of 'Port Gamble A' and another has 'PORT GAMBLE B'. A unit test that searches for 'gamble' fails to find either mocked record, and a search for 'Gamble' only finds one.

Right now, I'm just calling .ToLower() on the name field after deserializing to get around this, but there are many text fields in child objects I want to test searching on and calling .ToLower() on every single one is going to be a pain. Manually editing the JSON to lower case everything is even worse. I also don't want to change the LINQ query to call .ToLower() on the search term and database field because it seems silly to change a query just for unit tests, and it could have some performance consequences.

Is there a way to customize the serialization to force all string values to lower case? I found plenty of examples of how to lower case the property names, but not the actual values.

Aucun commentaire:

Enregistrer un commentaire