chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I am just starting to do unit testing

I use the following method

DataRepository.CommandesKitFaussesPortesVitresProvider.GetByCkfpId(pCommandesKitFaussesPortes.Id)
to return a list of objects

public Commande.Entities.TList<Commande.Entities.CommandesKitFaussesPortesVitres > GetByCkfpId(int _ckfpId) {
          int num = -1;
          return this.GetByCkfpId(_ckfpId, 0, 0x7fffffff, out num);
       }


I want to mock this and return a simple list i can leave empty or put in some objects...

I dont know if its because those are static

public static Commande.Data.Bases.CommandesKitFaussesPortesVitresProviderBase CommandesKitFaussesPortesVitresProvider
      {
         get
          {
             DataRepository.LoadProviders();
             return DataRepository._provider.CommandesKitFaussesPortesVitresProvider;
          } 
      }



but i cant find the proper syntax to mock the behavior
asked by RedSoxFred (1.8k points)

2 Answers

0 votes
Answering my own question, kind of.

what i did was refactor the call to the datarepository to use a public method that only did that

and then i was able to mock that method

so i created this

  Function GetListCommandeKitPourComId(pComID As Integer) As TList(Of CommandesKit)
            Return DataRepository.CommandesKitProvider.GetByComId(pComId)
        End Function


and mocked this
  Isolate.WhenCalled(() => fakePresenter.GetListCommandeKitPourComId(0)).WillReturn(list);


and created a faked list with some random items

I would still however like to know how to mock my original
answered by RedSoxFred (1.8k points)
0 votes
Hi,

To fake static members just use Isolate.WhenCalled method like this:
Isolate.WhenCalled( ()=> DataRepository.CommandesKitFaussesPortesVitresProvider.GetByCkfpId(0)).WillReturn(...);


If you want to fake all static methods use:
Isolate.Fake.StaticMethods(typeof(CommandesKitFaussesPortesVitresProvider));

This will cause all static methods and properties to return default values in case they return value types or fakes in case they return reference types.

Hope it helps.
answered by ohad (35.4k points)
...