Question

J'ai une méthode publique qui utilise une méthode privée locale pour obtenir des données de la Db.

private string SomeMethod(string) { ... Doing some operations ... string data = GetDBData(string); Doing some operations ... }

Je veux divert / isoler la méthode privée GetDBData (string) en utilisant les taupes donc mon test ne nécessite pas la DB.

, ma question est évidemment: comment le faire? Merci Uria

EDIT
Informations complémentaires:

j'ai essayé de changer la méthode accesseurs à la fois à la protection du public et interne, dans les deux cas, je peux maintenant voir les méthodes comme les taupes. MAIS lors du test, la méthode originale est encore utilisé et non le détour que j'ai mis dans le PexMethod.

Était-ce utile?

La solution 2

I figured it out

If i have the public MyClass with private SomeMethod

public class MyClass
{
    private string SomeMethod(string str){}
}

if you want to mole the SomeMethod method you need to use AllInstances in the test method:

[PexMethod]
SomeMethod(string str)
{
    MMyClass.AllInstances.SomeMethod = (instance, str) => { return "A return string"; };
}
  • notice that the lambda receives an instance parameter as the first parameter. I'm not sure what it's function is.

Autres conseils

You can try any of the following.

  • Make the method internal and add an attribute like this to the assembly:

    [assembly: InternalsVisibleTo("<corresponding-moles-assembly-name>")]

  • Change the method access to protected virtual and then use a stub.

  • Refactor your class so that it gets an interface (IDataAccessObject) as a constructor parameter, SomeMethod being one of the methods of that interface, and then pass a stub of that interface to your class in test methods.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top