testing - Faking a INI Configuration File Settings with FakeItEasy in C# -


i have class inherits abstract configuration class, , each class implements reader ini files, xml, conf, or proprietary formats. having problem in creating objects tested using fakeiteasy.

the object trying test uses configuration object via dependency injection, can read configuration settings calling readstring(), readinteger() etc... functions, , text location (section, key instance, ini) may retrieved appropriate section in whatever format of configuration file (ini, xml, conf, etc...).

sample code being used:

public class testfile {     private readonly configurationsettings configurationcontroller_ ;      ...      public testfile(configurationsettings configobject)     {         this.configurationcontroller_ = configobject;     }      public testfile(xmlfile configobject)     {         this.configurationcontroller_ = configobject;     }      public testfile(inifile configobject)     {         this.configurationcontroller_ = configobject;     }      ...      private list<string> getloginsequence()     {         list<string> returntext = new list<string>();         string signonfirst = configurationcontroller_.readstring("signon", "signonkeyfirst", "y");         string sendenterclear = configurationcontroller_.readstring("signon", "sendenterclear", "n");          if (sendenterclear.equals("y", stringcomparison.currentcultureignorecase))         {             returntext.add("enter");             returntext.add("clear");         }          if (signonfirst.equals("n", stringcomparison.currentcultureignorecase))         {             returntext.addrange(macrouserpassword("[$pass]"));             returntext.add("sign_on");         }         else         {             returntext.add("sign_on");             returntext.addrange(macrouserid("[$user]"));             returntext.addrange(macrouserpassword("[$pass]"));         }         return returntext;     } 

a simple test example:

    [testmethod]     public void testsignonsequence()          inireader fakeini = a.fake<inireader>();          //sample reads:         //config.readstring("signon", "signonkeyfirst", "y");         //config.readstring("signon", "sendenterclear", "n");  // section, key, default          a.callto(() => fakeini.readstring(a<string>.that.matches(s => s == "signon"), a<string>.that.matches(s => s == "sendenterclear"))).returns("n");         a.callto(() => fakeini.readstring(a<string>.that.matches(s => s == "signon"), a<string>.that.matches(s => s == "signonkeyfirst"))).returns("n");          a.callto(fakeini).where( x => x.method.name == "readinteger").withreturntype<int>().returns(1000);          testfile testfileobject = new testfile(fakeini);          list<string> returnedkeys = testfileobject.getloginsequence();         assert.areequal(2, returnedkeys.count, "ensure keystrokes returned"); 

this compiles fine, when execute code, following exception:

test threw exception: fakeiteasy.configuration.fakeconfigurationexception:     current proxy generator can not intercept specified method following reason:     - non virtual methods can not intercepted. 

if change how create fake works without exception, can not different values various calls same function.

a.callto(fakeini).where( x => x.method.name == "readstring").withreturntype<string>().returns("n"); 

the above method not allow me control return different calls function uses ini.

how can combine 2 methods, , test of parameters?

additional definitions requested:

public abstract class configurationsettings {     ...      abstract public int readinteger(string section, string key, int default);     abstract public string readstring(string section, string key, string default);      public int readinteger(string section, string key)     {   return readinteger(section, key, 0);    }      public int readinteger(string key, int default)     {   return readinteger("", key, default);   }      public int readinteger(string key)     {   return readinteger(key, 0);             }      public string readstring(string section, string key)     {   return readstring(section, key, null);  } }  public class inireader : configurationsettings {     ...      public inireader()     {     }      public inireader(string pathandfile)      {         this.pathandfilename = pathandfile;     }      public override int readinteger(string section, string key, int default)     {         return getprivateprofileint(section, key, default, pathandfilename);     }      public override string readstring(string section, string key, string default)     {         stringbuilder workingstring = new stringbuilder(max_entry);         int return = getprivateprofilestring(section, key, default, workingstring, max_entry, pathandfilename);         return workingstring.tostring();     } } 

you're getting

the current proxy generator can not intercept specified method following reason: - non virtual methods can not intercepted.

error because you're trying fake 2-parameter version of readstring. only virtual members, abstract members, or interface members can faked. since two-paremter readstring none of these, can't faked. think should either have virtual 2-parameter readstring or fake 3-parameter readstring.

your example pushes me towards faking 3-parameter readstring, since getloginsequence uses one. think constrain using expressions (rather method name strings) , work out.

i made little test bits of code (mostly before update) , had success faking 3-parameter readstring:

[test] public void blairtest() {     inireader fakeini = a.fake<inireader>();      a.callto(() => fakeini.readstring("signon", "sendenterclear", a<string>._)).returns("n");     a.callto(() => fakeini.readstring("signon", "signonkeyfirst", a<string>._)).returns("n");      // personally, i'd use above syntax 1 too, didn't     // want muck much.     a.callto(fakeini).where(x => x.method.name == "readinteger").withreturntype<int>().returns(1000);       testfile testfileobject = new testfile(fakeini);      list<string> returnedkeys = testfileobject.getloginsequence();     assert.areequal(2, returnedkeys.count, "ensure keystrokes returned"); } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -