A few points: 1. To mock a method on a class it must be marked as virtual (FindDataSetInDatabaseRegister is not virtual therefore you can't mock it). 2. Are you sure that's the method you want to mock? it sort of looks like you want to mock the calls to dp.xxxx within that message... if so then you need a way to replace the IDataProvider that is passed into the method. This is currently retrieved from DAL, which I guess is some sort of static? 3. All your test methods create a mock IDataProvider (db) but then never pass this mock into the target. Therefore the mock is never hit. You might want to use Dependency Injection to inject your mock (or real IDataProvider) into the target. Alternatively you could change your DAL class to allow your mock to be returned (I personally wouldn't do this) 4. Usually you wouldn't mock your target, you mock the dependencies of your target instead. 5. Why do you need a private accessor? can't you just create an instance of your target, then call ProcessFile on it (seeing as it's public) 6. A lot of what you've got there doesn't exactly look normal, try looking at Rhino Mocks documentation or some open source projects that use Rhino Mocks to see some examples of how to use it
Hope some of that helps. 2009/10/20 Chris <[email protected]> > > Hi, everyone. > > I'm pretty new to mocking and TDD. I work in VS2008 with MSTest and > Rhino.Mocks 3.6. > > Below is the class I'm working on and its test harness below it. I'm > trying to mock the method [FindDataSetInDatabaseRegister]. But I keep > getting the same exception with each approach I try. > > As you can see, I'm using the private accessor that MSTest creates. I > don't know if this is the main reason that my test keeps failing on > the setting up of the specific method call. > > In approach 1 and 2: if I don't have the mock.ReplayAll(); then the > IDataProvider mock is not used and the method setup fails. If the > [mock.ReplayAll();] is there, then, when the method is recorded, the > mock IDataProvider IS used and the method is partially recorded. Then > the exceptions (noted at the end) are thrown. > > In approach 4 ("TRY 4") the recording doesn't cause an error and the > actual test code is executed. But then the mocked method is not > substituted and the real method is called instead. > > Could anyone please assist me with this problem? > Thanks > Chris > > /////////////////////////////////////////////////////////////////////////////////////// > > public class WorkProcess > { > internal struct TargetDataSetStruct > { > public Guid DataSet_GUID; > public string ConnectionString; > public override string ToString() { return string.Empty; } > } > > > internal void RunChecks() > { > //check if the dataset_id > exists > try > { > TargetDataSet = FindDataSetInDatabaseRegister > (DataSet_ID, DAL.DataProvider.GetProvder()); > } > catch (Exception ex) > { > var exd = (ex as DataSetNotFoundException); > exd.Data.Add("comm_arg", "-d"); > exd.Data.Add("value", > DataSetID); > throw exd; > } > } > > internal TargetDataSetStruct FindDataSetInDatabaseRegister(int > DataSet_ID, IDataProvider dp) > { > try > { > TargetDataSetStruct tds = new TargetDataSetStruct(); > tds.DataSet_GUID = dp.ReturnGUIDFromIntegerMapping > (DataSet_ID); > tds.ConnectionString = > dp.ReturnConnectionStringFromIntegerMapping(DataSet_ID); > return tds; > } > catch (Exception e) > { > throw new DataSetNotFoundException("Dataset_ID [" + > DataSet_ID.ToString() + "] not found (or another error occured).", e); > } > } > > public void ProcessFile() > { > RunChecks(); > } > } > > ///////////////////////////////////////////////////////////////// > [TestClass()] > public class WorkProcessTest3 > { > > > private TestContext testContextInstance; > > /// <summary> > ///Gets or sets the test context which provides > ///information about and functionality for the current test > run. > ///</summary> > public TestContext TestContext > { > get > { > return testContextInstance; > } > set > { > testContextInstance = value; > } > } > > private WorkProcess_Accessor target; > private static MockRepository mock; > private static IDataProvider db; > private static int DataSet_ID = 1; > private static System.Guid exp_value = System.Guid.NewGuid(); > private static string exp_conn_string = "test"; > private static string loc = string.Empty; > > [ClassInitialize()] > public static void WorkProcessTestInitialize(TestContext > testContext) > { > HS3Integration.BusinessLayer.Shared.Globals.ApplicationDir > = System.Environment.CurrentDirectory; > > //setup the mock data provider > mock = new MockRepository(); > > using (System.IO.StreamWriter sw = > System.IO.File.CreateText(System.Environment.CurrentDirectory+"\ > \testfile.rsp")) > { > sw.Write("Some text"); > } > > loc = > HS3Integration.BusinessLayer.Shared.Globals.ApplicationDir; > } > > /// <summary> > ///A test for ProcessFile that tests whether the method > processes the correct file type > ///</summary> > [TestMethod()] > public void ProcessFileShouldProcessTheCorrectFileTypeTest() > { > /* //TRY 1 > target = mock.PartialMock<WorkProcess_Accessor>(); > > db = mock.StrictMock<IDataProvider>(); > SetupResult.For(db.ReturnGUIDFromIntegerMapping > (DataSet_ID)).Return(exp_value); > SetupResult.For(db.ReturnConnectionStringFromIntegerMapping > (DataSet_ID)).Return(exp_conn_string); > > mock.ReplayAll(); > > Expect.Call(target.FindDataSetInDatabaseRegister > (DataSet_ID, db)).Return( > new WorkProcess_Accessor.TargetDataSetStruct() > { DataSet_GUID = exp_value, ConnectionString = > exp_conn_string }); > mock.ReplayAll(); */ > > /* //TRY 2 > target = mock.PartialMock<WorkProcess_Accessor>(); > > db = mock.StrictMock<IDataProvider>(); > > Expect.Call(db.ReturnGUIDFromIntegerMapping > (DataSet_ID)).Return(exp_value); > LastCall.Repeat.Any(); > Expect.Call(db.ReturnConnectionStringFromIntegerMapping > (DataSet_ID)).Return(exp_conn_string); > LastCall.Repeat.Any(); > mock.ReplayAll(); > > Expect.Call(target.FindDataSetInDatabaseRegister > (DataSet_ID, db)).Return( > new WorkProcess_Accessor.TargetDataSetStruct() > { DataSet_GUID = exp_value, ConnectionString = exp_conn_string }); > mock.ReplayAll(); */ > > > /* //TRY 3 > target = MockRepository.GenerateMock<WorkProcess_Accessor> > (); > db = MockRepository.GenerateMock<IDataProvider>(); > > db.Stub(x => x.ReturnGUIDFromIntegerMapping > (DataSet_ID)).Return(exp_value); > db.Stub(x => x.ReturnConnectionStringFromIntegerMapping > (DataSet_ID)).Return(exp_conn_string); > > var t = new WorkProcess_Accessor.TargetDataSetStruct() > { DataSet_GUID = exp_value, ConnectionString = exp_conn_string }; > target.Stub(x => x.FindDataSetInDatabaseRegister > (DataSet_ID, db)).Return(t); */ > > > > // TRY 4 > target = MockRepository.GenerateMock<WorkProcess_Accessor> > (); > target.Stub(x => x.FindDataSetInDatabaseRegister > (DataSet_ID, HS3Integration.DAL.DataProvider.GetProvder())).Return( > new WorkProcess_Accessor.TargetDataSetStruct() > { DataSet_GUID = exp_value, ConnectionString = > exp_conn_string }); > > target.FileTypeCode = > HS3Integration.BusinessLayer.Shared.Constants.ftResponse; > string[] args = { "-p", loc, "-f", "testfile.rsp", "-t", > "rsp", "-d", "01", "-l", @"tinus01\mastermed\mastermed" }; > target.SetArguments(args); > target.ProcessFile(); > mock.VerifyAll(); > } > } > > > > TRY 1 > Error msg > Test method > > HS3_PMA_TestSuite.WorkProcessTest3.ProcessFileShouldProcessTheCorrectFileTypeTest > threw exception: System.InvalidOperationException: Invalid call, the > last call has been used or no call has been made (make sure that you > are calling a virtual (C#) / Overridable (VB) method).. > > Error Stack Trace > Rhino.Mocks.LastCall.GetOptions[T]() > Rhino.Mocks.Expect.Call[T](T ignored) > > HS3_PMA_TestSuite.WorkProcessTest3.ProcessFileShouldProcessTheCorrectFileTypeTest > () in E:\Projects\HS3_PMA_BO\HS3_PMA_TestSuite\WorkProcessTest.cs: > line 404 > > > > TRY 2 > Error msg > Test method > > HS3_PMA_TestSuite.WorkProcessTest3.ProcessFileShouldProcessTheCorrectFileTypeTest > threw exception: System.InvalidOperationException: Invalid call, the > last call has been used or no call has been made (make sure that you > are calling a virtual (C#) / Overridable (VB) method).. > > Error Stack Trace > Rhino.Mocks.LastCall.GetOptions[T]() > Rhino.Mocks.Expect.Call[T](T ignored) > > HS3_PMA_TestSuite.WorkProcessTest3.ProcessFileShouldProcessTheCorrectFileTypeTest > () in E:\Projects\HS3_PMA_BO\HS3_PMA_TestSuite\WorkProcessTest.cs: > line 423 > > > > TRY 3 > Error msg > > HS3_PMA_TestSuite.WorkProcessTest3.ProcessFileShouldProcessTheCorrectFileTypeTest > threw exception: System.InvalidOperationException: Invalid call, the > last call has been used or no call has been made (make sure that you > are calling a virtual (C#) / Overridable (VB) method).. > > Error Stack Trace > Rhino.Mocks.LastCall.GetOptions[T]() > R](T mock, Function`2 action) > R](T mock, Function`2 action) > > HS3_PMA_TestSuite.WorkProcessTest3.ProcessFileShouldProcessTheCorrectFileTypeTest > () in E:\Projects\HS3_PMA_BO\HS3_PMA_TestSuite\WorkProcessTest.cs: > line 436 > > > > TRY 4 > Doesn't throw the previous errors. But the mock isn't used at all. > The call gets through > to the DAL method instead of the mocked FindDataSetInDatabaseRegister- > method (which is invoked much earlier). > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en -~----------~----~----~----~------~----~------~--~---
