Hi,
There is special API for specifying a return value from a constructor.
we do have a mechanism for replacing created object during runtime (we call this scenario "mocking a future instance")
you can get more details in this
blog post.
Here is an example on how to do it.
public class DataTable
{
public int ReturnFive()
{
return 5;
}
}
public class DataView
{
internal DataView(DataTable table)
{
_Table = table;
}
private DataTable _Table;
public DataTable Table
{
get { return _Table; }
set { _Table = value; }
}
}
[TestClass]
public class DataViewTests
{
[TestMethod]
[Isolated]
public void RepalcingDataViewExample()
{
/// This will swap the next creation of a dataTable
DataTable fake = Isolate.Fake.Instance<DataTable>();
Isolate.SwapNextInstance<DataTable>().With(fake);
// setting an expectation of the fake table
Isolate.WhenCalled(() => fake.ReturnFive()).WillReturn(6);
//table2 will actualy be mocked.
DataTable table2 = new DataTable();
DataView dataView2 = new DataView(table2);
Assert.IsNotNull(dataView2.Table);
//calls to DataView2.Table will be mocked
Assert.AreEqual(6, dataView2.Table.ReturnFive());
}
}
:!: For simplicity I've replaced the real DataView and DataTable classes with simple one I've created in the example however the same syntax will work on all classes you use.
Hope this helps let me know if you have anymore questions.