This is a migrated thread and some comments may be shown as answers.

Calling Private generic method <T> with Private Accessor

3 Answers 142 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sagar
Top achievements
Rank 1
Sagar asked on 26 May 2016, 09:51 AM

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

 

3 Answers, 1 is accepted

Sort by
0
Svetlozar
Telerik team
answered on 30 May 2016, 02:55 PM
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
0
Sagar
Top achievements
Rank 1
answered on 31 May 2016, 02:35 PM

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

 

0
Svetlozar
Telerik team
answered on 02 Jun 2016, 11:17 AM
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
Tags
General Discussions
Asked by
Sagar
Top achievements
Rank 1
Answers by
Svetlozar
Telerik team
Sagar
Top achievements
Rank 1
Share this question
or