Calling Private generic method <T> with Private Accessor

4 posts, 0 answers
  1. Sagar
    Sagar avatar
    11 posts
    Member since:
    May 2014

    Posted 26 May Link to this post

    Hi,

      I am facing issue, while calling a Generic method with Private accessor. Below is my sample method which, i would like to Call.

    private void PopulateCollection<T>(List<IDataItem> fromCollection, ObservableCollection<T> toCollection)

    {  //some code }

     

    And my Test method is as below,

    [TestMethod]
    public void TestPopulateCollection()
    {
        var testInstance = new TestVM();
        var privateInstance = new PrivateAccessor(testInstance);
        //declare and set required parameters here for fromCollection & toCollection
        privateInstance.CallMethod("PopulateCollection", fromCollection, toCollection);
        //Assert
    }

     

    The above code throws exception, "Member not found". 

    Is there a possible resolution for it?

     

    Thank you,

    Sagar

     

  2. Svetlozar
    Admin
    Svetlozar avatar
    269 posts

    Posted 30 May Link to this post

    Hello,

    Thank you for contacting us.

    Usually the member not found exception is thrown when the JustMock matcher couldn't find the member to be invoked.The matcher takes into account the actual arguments that you've passed. That works for me

      
    var testInstance = new Foo();
    var privateInstance = new PrivateAccessor(testInstance);
    var fromCollection = new List<IDataItem>();
    var toCollection = new ObservableCollection<IDataItem>();
    privateInstance.CallMethod("PopulateCollection", fromCollection, toCollection);

    Could you pleas send us a complete sample? It looks like our matcher can't find the member with your arguments.

    //declare and set required parameters here for fromCollection & toCollection
    We need your actual arguments so we can help.

    Regards,
    Svetlozar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Sagar
    Sagar avatar
    11 posts
    Member since:
    May 2014

    Posted 31 May in reply to Svetlozar Link to this post

    Hi Svetlozar,

     Below is my test method, in which i am facing the issue. 

     

    [TestMethod]
    public void TestPopulateCollection()
    {
               //Arrange                       
               var viewmodelLocator = new ViewModelLocator();
               var testVM = viewmodelLocator.TestVM;
               var privateTestVM = new PrivateAccessor(testVM);
     
               List<IDataItem> fromCollection = new List<IDataItem>();
               var di = Mock.Create<IDataItem>();
               Mock.Arrange(() => di.ItemKey).Returns("di1");
               fromCollection.Add(di);
     
               di = Mock.Create<IDataItem>();
               Mock.Arrange(() => di.ItemKey).Returns("di2");
               fromCollection.Add(di);
     
               ObservableCollection<TestLocationVM> toCollection = new ObservableCollection<TestLocationVM>();
     
               
               //Act
               privateTestVM.CallMethod("PopulateCollection", fromCollection, toCollection);
     
               //Assert
               Assert.AreEqual(2, toCollection.Count);
               
           }

     

    Could you please help me in getting the resolution?

     

    Thanks in advance,

    Sagar

     

  5. Svetlozar
    Admin
    Svetlozar avatar
    269 posts

    Posted 02 Jun Link to this post

    Hello,

    We would really like to help you, but we can't reproduce the issue on our side. Here is what I added to your sample code to make it a complete sample.

    public class ViewModelLocator
    {
        public TestLocationVM TestVM = new TestLocationVM();
    }
     
    public class TestLocationVM
    {
        private void PopulateCollection<T>(List<IDataItem> fromCollection, ObservableCollection<T> toCollection) where T : new()
        {
            foreach(var i in fromCollection)
            {
                toCollection.Add(new T());
            }
        }
    }
     
    public interface IDataItem
    {
        string ItemKey { get; set; }
    }


    I guess there is something missing in our code. Could you please prepare a simple self-contained solution that demonstrates the problem and send it over? Feel free to open a private support ticket if you don;t want the sample project to be public in the forum. 

    Regards,
    Svetlozar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top