Monday 15 April 2013

c# - Using Microsoft Fakes to Mock -


I have closed classes with Ashink (NIT 4.5), which need to make fun of the methods. I am using Microsoft Fox and so they will be "Shim". The following code is an example of what I need to do. It creates, but when the "login" synchronization method is run and the "login acic" method is called, the test is hanging.

  [TestMethod] Public async work log Post_Returns () {(ShimsContext.Create ()) {var c = New TestController (); Var user = new user (); Fakes.ShimUserManager.AllInstances.LoginAsyncString = (Um, U) => New Job & lt; IUser & gt; (() => {Return user;}); // call controller method var result = c. See login (model, return url) as a view; Var expectedViewName = "index"; Assert.IsNotNull (results); Confirm. First (expected ViewName, result. ViewName); } // Controller method public async works & lt; ActionResult & gt; Login (login model model, string return url) {var user = user manager. Login async (model username, model password); See Return (); }    

Task Do not use constructor Async code if you need a complete work , with return value, Tasks. Use the FRRScult :

  IUser user = new user (); Fakes.ShimUserManager.AllInstances.LoginAsyncString = (Um, U) => Task.FromResult (user);   

As an additional tip, these unit tests have a good idea to cover these cases:

  • Synchronous success ( Task.FromResult (user)
  • Asynchronous success ( Task .RUn () => User) ).
  • Asynchronous error ( Task.Run ((=) => new invalid operation expiration ("or whatever"); return user;}) ).

No comments:

Post a Comment