Hi,
Welcome to TypeMock.
It is quite hard to understand what you are testing, so it is hard to know how to mock it.
In your example I would not use mocks at all, but create a collection and run the test.
[TestMethod()]
public void IsMatchStreetTest()
{
string toMatch = "test2";
User user = new User();
user.Addresses.Clear();
user.Addresses.Add(new Address("test1"));
user.Addresses.Add(new Address(toMatch));
bool expected = true;
bool actual;
actual = MockTestProject.MockCollectionTestConsole_ProgramAccessor.IsAddressValid(user, toMatch);
Assert.AreEqual(expected, actual, "MockCollectionTestConsole.Program.IsMatchStreet did not return the expected value");
}
}
If you cannot access the Collection Field you can use TypeMocks StateObject to change the field in the object.
User user = new User();
ObjectState objState = new ObjectState(user);
AddressCollection addresses = objState.GetField("_addresses ");
addresses.Clear();
addresses.Add(new Address("test1"));
addresses.Add(new Address(toMatch));
It is also possible to mock the Addresses Property. Here is a NaturalMocks way to do it:
User user = new User();
AddressCollection fakeAddresses = new AddressCollection();
fakeAddresses .Clear();
fakeAddresses.Add(new Address("test1"));
fakeAddresses.Add(new Address(toMatch))
// switch Addresses with fake address
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
recorder.ExpectAndReturn(
user.Addresses
fakeAddresses);
}