Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,

I have to a question on how could I call a method within the same method inside my mockrespository.setup on MOQ. Below is my actual code where I need to call my method 'ChangeFilterAttribute inside the same method of 'ChangeFilterAttribute'.

Or any one can give me asample scenario that calling method inside the same method name in moq unit testing.

Thank you in advance for your kind help.

What I have tried:

C#
private void GetFilters_Initialize()
       {
           _mockRepository.Setup(p => p.ChangeFilterAttribute(It.IsAny<IList<IFilterDescriptor>>(), It.IsAny<Dictionary<string, Dictionary<string, Dictionary<string, int>>>>()))
                           .Returns((IList<IFilterDescriptor> filters, Dictionary<string, Dictionary<string, Dictionary<string, int>>> dictionaryColumnValue) =>
           {

               if (filters.Any())
               {
                   foreach (var filter in filters)
                   {
                       var descriptor = filter as FilterDescriptor;
                       foreach (var item in dictionaryColumnValue)
                       {
                           if (descriptor != null && item.Key == descriptor.Member)
                           {
                               foreach (var memVal in item.Value)
                               {
                                   descriptor.Member = memVal.Key.ToString();
                                   foreach (var conVal in memVal.Value)
                                   {
                                       if (descriptor.ConvertedValue.Equals(conVal.Key))
                                       {
                                           descriptor.Value = conVal.Value;
                                           descriptor.Operator = FilterOperator.IsEqualTo;
                                           break;
                                       }
                                       else
                                           descriptor.Operator = FilterOperator.Contains;
                                   }
                               }
                           }
                           else if (filter is CompositeFilterDescriptor)
                           {
                               var compositeFilterDescriptor = filter as CompositeFilterDescriptor;
                               if (compositeFilterDescriptor != null)
                               {
                                 // This is my problem, on how could I call this inside of this changefilterAtribute method in my unit testing
                                 ChangeFilterAttribute(compositeFilterDescriptor.FilterDescriptors, _changeFilterAttribute_DictonaryColumnValues);

                               }
                           }
                       }
                   }
               }
               return filters;


           });
       }
Posted
Updated 24-Nov-16 22:48pm
v3
Comments
Silver Lightning 23-Nov-16 23:47pm    
Could someone guide me on how could I use Callback in unit test MOQ Recursion in C#? Thank you in advance

I would think you would have to write a recursive method that wraps the call to ChangeFilterAttribute.
 
Share this answer
 
Comments
Silver Lightning 15-Nov-16 18:20pm    
You mean, I need to write new method inside the ChangeFilterAttribute? can you give me sample pseudocode or something? Thank you very much
Silver Lightning 17-Nov-16 18:24pm    
Can anyone help me on how could I implement callback in MOQ in recursive method. (Calling the same method inside this method)? Thank you in advance.
Solved!

C#
private void GetFilters_Initialize()
      {
          var myFilters = new Object();
          _mockRepository.Setup(p => p.ChangeFilterAttribute(It.IsAny<IList<IFilterDescriptor>>(), It.IsAny<Dictionary<string, Dictionary<string, Dictionary<string, int>>>>()))

          .Callback((IList<IFilterDescriptor> filters, Dictionary<string, Dictionary<string, Dictionary<string, int>>> dictionaryColumnValue) =>
              {
                  foreach (var filter in filters)
                      if (filter is CompositeFilterDescriptor)
                      {
                          var compositeFilterDescriptor = filter as CompositeFilterDescriptor;
                          if (compositeFilterDescriptor != null)
                          {
                              myFilters = compositeFilterDescriptor.FilterDescriptors;
                              return ;

                          }
                      }
                      else
                      {
                          myFilters = filters;
                          return;
                      }

              })
               .Returns((IList<IFilterDescriptor> filters, Dictionary<string, Dictionary<string, Dictionary<string, int>>> dictionaryColumnValue) =>
                          {

                              filters = myFilters as IList<IFilterDescriptor>;
                              if (filters.Any())
                              {
                                  foreach (var filter in filters)
                                  {
                                      var descriptor = filter as FilterDescriptor;
                                      foreach (var item in dictionaryColumnValue)
                                      {
                                          if (descriptor != null && item.Key == descriptor.Member)
                                          {
                                              foreach (var memberValue in item.Value)
                                              {
                                                  descriptor.Member = memberValue.Key.ToString();
                                                  foreach (var convertedValue in memberValue.Value)
                                                  {
                                                      if (descriptor.ConvertedValue.Equals(convertedValue.Key))
                                                      {
                                                          descriptor.Value = convertedValue.Value;
                                                          break;
                                                      }
                                                  }
                                              }
                                          }
                                      }
                                  }
                              }
                              return filters;

                          });

      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900