Hi Patrick,
Thanks for the reply! I'm a big fan of your writings.
Details: I have a wcf web service and the interface has lots of stuff
in it, but I am mainly interested in making a call to one of these
services to have it get data from the database and return it to my SL4
application. That part of the interface includes:
public interface IDataService : IMACSWcfService
{
...
void
GetStopEventsAsync(System.Collections.Generic.Dictionary<string,
string> param, object userState);
event System.EventHandler<GetStopEventsCompletedEventArgs>
GetStopEventsCompleted;
...
}
I have a search class that will need to call this DataService async
and in the production code I will need to do something like this:
1. Pass in the IDataService implementation using constructor injection
to the Search class
2. Save this locally in the Search class
3. Create a method in Search that will call the ws to get the data
4. When the data is returned an event is raised and the callback is
executed
5. The callback saves the returned data
If want to TDD this with Rhino/MSTest then
private MockRepository mockRepository;
private IDataService mockDS;
[TestMethod]
public void SearchForStopEventsShouldCallGetStopEventsAsync()
{
// Arrange:
mockRepository = new MockRepository();
mockDS = mockRepository.StrictMock<IDataService>();
Search sut = new Search(mockDS);
// Act:
sut.GetStopEvents(GlobalVariables.SearchType.ByLoadId);
// Assert:
RhinoMocksExtensions.AssertWasCalled(mockDS.GetStopEventsAsync(null,
null));
}
[TestMethod]
public void
SearchForStopEventsShouldRegisterCallbackEvent_OnDataReturned()
{
// Arrange:
mockRepository = new MockRepository();
mockDS = mockRepository.StrictMock<IDataService>();
Search sut = new Search(mockDS);
// Act:
sut.GetStopEvents(GlobalVariables.SearchType.ByLoadId);
// Assert:
}
[TestMethod]
public void
SearchForStopEventsShouldCallbackEvent_OnDataReturned_WhenEventIsRaised()
{
// Arrange:
mockRepository = new MockRepository();
mockDS = mockRepository.StrictMock<IDataService>();
Search sut = new Search(mockDS);
// Act:
sut.GetStopEvents(GlobalVariables.SearchType.ByLoadId);
// Assert:
}
Here is what my production code will look like:
public class Search : ISearch
{
private readonly IDataService _dataService;
public Search(IDataService ds)
{
_dataService = ds;
}
public void GetStopEvents(GlobalVariables.SearchType st)
{
try
{
_dataService.GetStopEventsCompleted +=
OnDataReturned; ;
_dataService.GetStopEventsAsync(_param, st);
}
catch (Exception ex)
{
if (!ViewModelLocator.IsMACSAdjustmentVMNull)
{
ViewModelLocator.MACSAdjustmentViewModelStatic.ReturnedMessage =
ex.Message;
}
throw new Exception(ex.Message);
}
void OnDataReturned(object sender,
GetStopEventsCompletedEventArgs e)
{
var args = new GetDataDoneInSearchModelArgs();
if (e.Result != null && e.Result.ToList().Count > 0)
{
_stopEventsDataListReturned =
e.Result.Select(dtoStopEvent => new Stop(dtoStopEvent)).ToList();
...
}
else
{
args.Message = "No Data Found";
}
}
Clearly, I have no idea where to start with Rhino,
1) whether I should be using a strictmock or a stub.
2) how to check that a method on the mock was called
3) how to check if the even has been registered
4) how to raise the event
5) how to check if the callback was called.
I find so many different syntaxes in examples using Rhino probably
because of all of the different APIs that have existed over time. I'd
like to be able to use the AAA Syntax if possible but am totally lost
when it comes to how.
thanks much!
Bill Campbell
(twitter: bill44077)
On Nov 30, 10:52 am, Patrick Steele <[email protected]> wrote:
> I've never mocked anything like this before either, but I have used
> Rhino Mocks before. Do you have some sample code that you could show?
> Something concrete like your interface definition and how you want
> things mocked out?
>
> ---
> Patrick Steelehttp://weblogs.asp.net/psteele
>
>
>
>
>
>
>
> On Sat, Nov 27, 2010 at 3:41 PM, Bill44077 <[email protected]> wrote:
> > Hi,
> > I've been trying to figure out how to mock an async wcf call mocking
> > out the interface using RhinoMocks. I can't seem to be able to figure
> > out how to do this using the AAA syntax (not playback/record), mocking
> > raising the callback event, using lambdas, receiving the result object
> > from the mock. I basically want to mock out this whole collaboration.
> > I'm quite sure this is possible but can't figure out the syntax/calls.
> > If anyone has an example of how to do this - I'd really appreciate it.
> > I would think this would be a very common set of tests when doing TDD
> > with Silverlight since everything with the server is an async call.
> > Thanks in Advance!
> > Bill Campbell
>
> > --
> > 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
> > athttp://groups.google.com/group/rhinomocks?hl=en.
--
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.