jamesmontemagno / SettingsPlugin

Read and Write Settings Plugin for Xamarin and Windows
MIT License
324 stars 80 forks source link

[Feature] Use Interface to be able make mockable tests #70

Closed AlirezaAkbarix closed 7 years ago

AlirezaAkbarix commented 7 years ago

I'm using this nice lib as my setting handler, the problem is that as there is no interface, I cannot mock it (e.g. to verify if some items are changing with specific data).

also after changing the class that is auto-generated to code below, I couldn't inject it to my project :((

`public class Settings : IAppSettings { private static ISettings AppSetting => CrossSettings.Current;

    #region Settings Constants
    public const string BaseUrlKey = "baseurl";
    public static readonly string BaseUrlDefault = "http://localhost:88/";
    public const string JustAvailableProductKey = "justAvailable";
    public static readonly bool JustAvailableDefault = false;

    #endregion

    public void SetValue(string key, int value)
    {
        AppSetting.AddOrUpdateValue(key, value);
    }

    public void SetValue(string key, string value)
    {
        AppSetting.AddOrUpdateValue(key, value);
    }

    public void SetValue(string key, long value)
    {
        AppSetting.AddOrUpdateValue(key, value);
    }

    public void SetValue(string key, float value)
    {
        AppSetting.AddOrUpdateValue(key, value);
    }

    public void SetValue(string key, bool value)
    {
        AppSetting.AddOrUpdateValue(key, value);
    }

    public void SetValue(string key, decimal value)
    {
        AppSetting.AddOrUpdateValue(key, value);
    }

    public int GetValue(string key, int defaultValue)
    {
        return AppSetting.GetValueOrDefault(key, defaultValue);
    }

    public string GetValue(string key, string defaultValue)
    {
        return AppSetting.GetValueOrDefault(key, defaultValue);
    }

    public float GetValue(string key, float defaultValue)
    {
        return AppSetting.GetValueOrDefault(key, defaultValue);
    }

    public long GetValue(string key, long defaultValue)
    {
        return AppSetting.GetValueOrDefault(key, defaultValue);
    }

    public bool GetValue(string key, bool defaultValue)
    {
        return AppSetting.GetValueOrDefault(key, defaultValue);
    }

    public decimal GetValue(string key, decimal defaultValue)
    {
        return AppSetting.GetValueOrDefault(key, defaultValue);
    }
}`

And IAppSetting.cs:

public interface IAppSettings { void SetValue(string key, int value); void SetValue(string key, string value); void SetValue(string key, float value); void SetValue(string key, long value); void SetValue(string key, bool value); void SetValue(string key, decimal value); int GetValue(string key, int defaultValue); string GetValue(string key, string defaultValue); float GetValue(string key, float defaultValue); long GetValue(string key, long defaultValue); bool GetValue(string key, bool defaultValue); decimal GetValue(string key, decimal defaultValue); }

Now I can mock setting I wish in my tests and verify if methods are called with specific items.

thanks for your helpful lib :)

jamesmontemagno commented 7 years ago

See: http://motzcod.es/post/159267241302/unit-testing-plugins-for-xamarin

There already is an interface.