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
Hi!

I have the following method in my class -

public void MyMethod()
        {
//[code snipped]
            BackgroundWorker bw = new BackgroundWorker
                                      {
                                          WorkerReportsProgress = false,
                                          WorkerSupportsCancellation = false
                                      };

            bw.DoWork += (sender, args) =>
                             {
                                 while (!context.PromoCodeDefinitions.IsSafeToExit())
                                 {                                    
                                     Thread.Sleep(10);
                                 }
                             };
            bw.RunWorkerCompleted += (sender, args) => File.Delete(tempFile);
            bw.RunWorkerAsync();
        }


I am trying to mock some private methods in that class but I am getting the following error -
TypeMock.TypeMockException: *** No method with name MyMethod>b__5 in type MyClass exists..

This happens on every Isolate.NonPublic. call of this class. Is there a workaround to fake private methods of the class containing this method without me having to rewrite the code in MyMethod?

Thanks.

p.s. Using Isolator 5.1.2
asked by vladk (680 points)

5 Answers

0 votes
Hi

Can you please post the test code and the signature of the method you are trying to fake?
answered by ohad (35.5k points)
0 votes
Here's the actual method -

public void ImportPromoCodes(ConsumablePromoCode promoCode, List<string> promoCodes)
        {          
            string tempFile = Path.GetTempFileName();
            promoCodes.Insert(0, "CouponCode");
            File.WriteAllLines(tempFile, promoCodes.ToArray());
            string batchName = "ImportBatch" + GetTimeStamp();
            context.PromoCodeDefinitions.ImportCodes(promoCode.Id, batchName, tempFile);

            BackgroundWorker bw = new BackgroundWorker
                                      {
                                          WorkerReportsProgress = false,
                                          WorkerSupportsCancellation = false
                                      };

            bw.DoWork += (sender, args) =>
                             {
                                 while (!context.PromoCodeDefinitions.IsSafeToExit())
                                 {                                     
                                     Thread.Sleep(10);
                                 }
                             };
            bw.RunWorkerCompleted += (sender, args) => File.Delete(tempFile);
            bw.RunWorkerAsync();
        }


Now here's a sample test -

       
 [TestMethod, Isolated]
 public void RetrieveDiscountList_Positive_Empty()
        {
            MarketingSystem marketingSystem = new MarketingSystem(SiteName);

            Isolate.NonPublic.WhenCalled(marketingSystem, "GetAllDiscountIds").WillReturn(new List<int>());
            List<Discount> discounts = marketingSystem.RetrieveAllDiscounts();

            Assert.AreEqual(discounts.Count, 0);
        }


The error gets thrown at Isolate.NonPublic.WhenCalled line. This test has nothing to do with ImportPromoCode but because GetAllDiscountIds is in the same class the mentioned error gets thrown.

*** No method with name ImportPromoCodes>b__5 in type NamespaceRemoved.MarketingSystem exists..
answered by vladk (680 points)
0 votes
Hi,

Could it be by any chance that the method GetAllDiscountIds is a static method?
If it is use the type that holds the method as the first argument of WhenCalled instead of the instance.

Example:
public class SomeClass
{
    private static List<int> GetAllDiscountIds()
    {
        return new List<int>();
    }
}

// Test method
[TestMethod]
public voidRetrieveDiscountList_Positive_Empty()
{
    Isolate.NonPublic.WhenCalled(typeof(SomeClass), "GetAllDiscountIds").WillReturn(new List<int>());
}


Please let me know if it helps.
answered by ohad (35.5k points)
0 votes
Hi, sorry I should have mentioned method signature -

        private List<int> GetAllDiscountIds(int campaignId)


It is not a static method.
answered by vladk (680 points)
0 votes
Hi

Lets take it offline to investigate it.
I'll send you mail from our support.
answered by ohad (35.5k points)
...