This is possible but I don't think what is being tested is really related to the question. It appears as though you are trying to test that the method is called twice but only if the result of the first invocation should result in a second invocation.
In other words, the behavior is more important than the data. So you could simply remove the second mock and change the "Repeat" from "Once" to "Twice" This would basically say that you are expecting the "Invoke" method to be called two times. According to the code the parameters don't change between the first call and the second call so... setting up expectations based on different arguments (or parameters) won't work. The "anti" test to this is to have the "dataLength" return any value greater than zero. The expectation (or behavior) would be a single call. Obviously "Repeat" would be "Once." I (personally) would also change the generation of the mock from "GenerateStub" to "GenerateMock" Mocks that are generated as "stubs" are not expected to have expectations. This is a construct to identify (or describe) the intent of the "mock." On Tuesday, July 2, 2013 8:37:59 AM UTC-4, [email protected] wrote: > > I am trying to test the following method for the case where 'the > dataRequestCommand' is executed twice. > > /// <summary> > /// Gets the peripheral data. > /// </summary> > /// <param name="dataRequestCommand">The data request command.</param> > /// <returns>The data received from the peripheral.</returns> > public virtual IList<byte> > GetPeripheralData(PeripheralDataRequestCommanddataRequestCommand) > { > int status; > byte *dataLength*; > IList<byte> data; > dataRequestCommand.Invoke(out status, out *dataLength*, out data); > if (*dataLength* == 0) > { > Thread.Sleep(this.PeripheralDataRetrievalWaitTime); > dataRequestCommand.Invoke(out status, out *dataLength*, out data); > } > > return data; > } > > Currently my unit test looks as follows: > > /// <summary> > /// Tests GetPeripheralData() with no data on first call. > /// </summary> > [Test] > public void > TestGetPeripheraldataWithNoDataOnFirstCallExpectCalledTwiceAndDataReturn() > { > // arrange > var firstGet = MockRepository.GenerateStub<FirmwareCommandProcessor. > PeripheralDataRequestCommand>(); > var secondGet = MockRepository.GenerateStub<FirmwareCommandProcessor. > PeripheralDataRequestCommand>(); > IList<byte> *data*; > byte *length*; > int *callStatus*; > firstGet > .Expect(g => g.Invoke(out *callStatus*, out *length*, out *data*)) > .OutRef(0, (byte)0, new List<byte>()) > .Repeat.Once(); > int *secondStatus*; > byte *secondLength*; > IList<byte> *secondData*; > secondGet > .Expect(g => g.Invoke(out *secondStatus*, out *secondLength*, out * > secondData*)) > .OutRef(0, (byte)2, new List<byte> { 1, 2 }) > .Repeat.Once(); > > // act > var result = this.commandProcessor.GetPeripheralData(firstGet); > > // assert > firstGet.VerifyAllExpectations(); > secondGet.VerifyAllExpectations(); > //// NOTE: need to determine how to capture the output on the second call. > //// Rhino mocks returns first call values as the test is currently > written. > ////Assert.IsNotNull(result); > ////Assert.IsInstanceOf<IList<byte>>(result); > ////var output = result as IList<byte>; > ////Assert.AreEqual(2, output.Count); > ////Assert.AreEqual(1, output[0]); > ////Assert.AreEqual(2, output[1]); > } > > Ideally I would like accomplish this with a single mock object for > FirmwareCommandProcessor.peripheralDataRequestCommand. I have not been able > to figure out how to change the OutRef parameters of 0, (byte)0, and new > List<byte> used for the first call to 0, (byte)2, new List<byte> { 1, 2 } > in the second call. So I'm asking, first, if this is even possible. If it > is, how would I do it? > > Thank you. > -- You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/rhinomocks. For more options, visit https://groups.google.com/groups/opt_out.
