For a project I am using a bunch of custom Personalization rules, the only problem I am facing at the moment is that it takes quite a while to load the rules and I can't find any good examples of a unit test for a custom rule like one of these:

 public class TemperatureHigherThanRule<T> : StringOperatorCondition<T> where T : RuleContext
    public string Value { get; set; }

    protected override bool Execute(T ruleContext)
        ////Convert the string Value to and int 
        int intValue = Convert.ToInt32(Value);

        ////Get the current weather with the API 
        HttpClient httpClient = new HttpClient();
        var response = httpClient.GetAsync("http://api.openweathermap.org/data/2.5/weather?q=Tilburg,nl&units=metric&APPID=***********").Result;

        var responseContent = response.Content;

        var json = responseContent.ReadAsStringAsync().Result;

        var currentWeather = Newtonsoft.Json.JsonConvert.DeserializeObject<CurrentWeather>(json);

        //Check if the parameter is higher than the current weather 
        return (currentWeather.main.temp > intValue);

Does anyone have a good example? Already had a look a these:




  • There is not much logic in you code so what is it exactly you want to test ? Not that you shouldn't test but can you be more specific in what is giving you problems. Commented Mar 6, 2018 at 8:47

1 Answer 1


This is not a Unit Testing problem

This is just bad architecture.

You need to isolate your calls to the weather service. Put that in a background service, that will call for new weather information every... 30 minutes or so. And if you need to vary the calls "by city" or whatever - at the very least make sure to cache the results in the same background service, so it only gets called once (in that 30 minute timespan).

Then, in your actual rule, call up the background service (which should respond with a cached result almost instantly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.