c# - Unit Test OData in Web API Using Moq -


I am trying to test unit a GET method in a Web API controller method A ODataQueryOptions parameter. I need to verify the OData filter result and I do not know how to argue what I say? Am i checking it properly

I am using this as an inspiration:

edit : Do I have to assure this for a valid test? It is returned with my TryGetContentValue call. If so, how do I do it?

  {system.clalls. Generic List 1 [CSR.Service.Models.CSRRole]. Where ($ it => ($ it RoleName == value (system web.ttp.data.query.expressions linchpremeter container + typed linakpamator Container '1 [system.STRING]). Property typed)) }  

Unit Test

  [TestMethod] Public Zero GetTestWithOData () {// Win Request = New HttpRequestMessage (HttpMethod.Get, "http: // Localhost: 21584 / api / test? $ Filter = RoleName% 20eq% 20 'user' "); Odata ModelBuilder Modelbilder = New ODataConventionModelBuilder (); ModelBuilder.EntitySet & LT; CSRRole & gt; ("CSRRoles"); Var opts = New ODataQueryOptions & lt; CSRRole & gt; (New ODataQueryContext (modelBuilder.GetEdmModel (), typef (CSRRole)), request); Var Yewmock = new fake & lt; ICSRUnitOfWork & gt; (); UowMock.SetupGet (i = & gt; i.TestRepository). Returns (New Imitation & Lieutenant IGenericRepository & lt; CSRRole, CSRContext & gt; & gt; (.) Object); Var Controller = New TestController (uowMock.Object); Controller Request = New HttpRequestMessage (); Controller request. Properties Add (new HTTP configuration (HTTP) for HTTPProperty; // Act Vari Results = Controller (Opt); // Modified IQueryable & lt; CSRRole & gt; Roles; Assert.IsNotNull (results); // **** I do not think this is a good test **** fears. ITRT (ResultTigateContent Values ​​& lt; IQueryable & lt; CSRRole & gt; & gt; (Roles outside); }  

Controller method

  Receive public HTTP response message (system web..http.data.data.eduation & lt; CSRRole & gt; option) { HttpResponseMessage feedback; If (option == faucet) {return response = new HTTPRPS pessmuse (HTTTPTcod.badrequest); } Var result = options.ApplyTo (_csrUnitOfWork.TestRepository.Get ()); If (result == empty) {response = new HTTPPassmessage (HTTPetus code.Notfond); } Other {response = Request.CreateResponse (HttpStatusCode.OK, results); Response.Content.Headers.Expires = New DateTime Offset (Datetime. Now.AddSeconds (300)); } Return response; }  

You also need to copy opts . For example:

  var opts = new mock & lt; IDATAquoctions & gt; (); Opts.Setup (m = & gt; m.ApplyTo (Anyone & lt; / * takes it in whatever class * / & gt; ()) Returns (/ * Whatever is required to return / /);  

Notifications work on an interface so that you will also need to create it. With a junky object, you can return it to whatever unit you need for the test Ask for.


Comments

Popular posts from this blog

Active Directory Authenticated Proxy Server with Squid or CNTLM -

java - Can't add JTree to JPanel of a JInternalFrame -

python - Calculate STD manually using Groupby Pandas DataFrame -