Domanda

Ho un metodo pubblico che utilizza un metodo privato locale per ottenere i dati dal DB.

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

Voglio deviazione / isolare il metodo privato GetDBData (string) utilizzando talpe quindi il mio test non richiede la DB.

Ovviamente, la mia domanda è: come si fa? grazie Uria

EDIT
Ulteriori informazioni:

ho provato a cambiare il metodo di accesso sia al pubblico e interna protetta, in entrambi i casi posso ora vedere i metodi come talpe. MA quando si esegue il test, il metodo originale è ancora in uso e non la deviazione ho implementato nel PexMethod.

È stato utile?

Soluzione 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.

Altri suggerimenti

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.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top