c# - Unit Testing MongoDB WriteOneAsync() Fails to create document -


i'm tad perplexed writing unit test test mongodb writeoneasync(). have tried running unit test on mongodb example writes document mongodb failing write document database.

below code. tried awaiting mc.savetomongo(); cannot await void.

this example put demo problem. actual code uses interface , data layer same problem.

i should code works fine if call savetomongo() within own project class or form project. problem seems exists when unit testing.

any woiuld massivly appreciated!

many thanks

i have unit test project follows,

using system; using microsoft.visualstudio.testtools.unittesting; using mongotestclass;  namespace unittestproject1 {     [testclass]     public class unittest1     {         [testmethod]         public async void testmethod1()         {             mongoclass mc = new mongoclass();             mc.savetomongo();         }     } } 

i have seperate class project containing following.

using mongodb.bson; using mongodb.driver; using system;  namespace mongotestclass {     public class mongoclass     {         protected static imongoclient _client;       protected static imongodatabase _database;          public async void savetomongo()         {             uri con;             uri.trycreate("mongodb://" + "desktop-263rhtl:27017", urikind.absolute, out con);              _client = new mongoclient(new mongoclientsettings             {                 connecttimeout = new timespan(0, 0, 0, 5, 0),                 server = new mongoserveraddress(con.host, con.port)             });              _database = _client.getdatabase("schematest3");              var document = new bsondocument             {                 { "address" , new bsondocument                     {                         { "street", "2 avenue" },                         { "zipcode", "10075" },                         { "building", "1480" },                         { "coord", new bsonarray { 73.9557413, 40.7720266 } }                     }                 },                 { "borough", "manhattan" },                 { "cuisine", "italian" },                 { "grades", new bsonarray                     {                         new bsondocument                         {                             { "date", new datetime(2014, 10, 1, 0, 0, 0, datetimekind.utc) },                             { "grade", "a" },                             { "score", 11 }                         },                         new bsondocument                         {                             { "date", new datetime(2014, 1, 6, 0, 0, 0, datetimekind.utc) },                             { "grade", "b" },                             { "score", 17 }                         }                     }                 },                 { "name", "vella" },                 { "restaurant_id", "41704620" }             };              var collection = _database.getcollection<bsondocument>("restaurants");             await collection.insertoneasync(document);         }     } } 

the problem lays in fact test ends prematurely, due fact you're not awaiting on of asynchronous tasks. thus, have race condition between insertion of document , termination of test.

what need expose async task operation, instead of async void, allow await call chain of test:

public task savetomongoasync() {     uri con;     uri.trycreate("mongodb://" + "desktop-263rhtl:27017", urikind.absolute, out con);      _client = new mongoclient(new mongoclientsettings     {         connecttimeout = new timespan(0, 0, 0, 5, 0),         server = new mongoserveraddress(con.host, con.port)     });      _database = _client.getdatabase("schematest3");      var document = new bsondocument     {         { "address" , new bsondocument             {                 { "street", "2 avenue" },                 { "zipcode", "10075" },                 { "building", "1480" },                 { "coord", new bsonarray { 73.9557413, 40.7720266 } }             }         },         { "borough", "manhattan" },         { "cuisine", "italian" },         { "grades", new bsonarray             {                 new bsondocument                 {                     { "date", new datetime(2014, 10, 1, 0, 0, 0, datetimekind.utc) },                     { "grade", "a" },                     { "score", 11 }                 },                 new bsondocument                 {                     { "date", new datetime(2014, 1, 6, 0, 0, 0, datetimekind.utc) },                     { "grade", "b" },                     { "score", 17 }                 }             }         },         { "name", "vella" },         { "restaurant_id", "41704620" }     };      var collection = _database.getcollection<bsondocument>("restaurants");     return collection.insertoneasync(document); } 

and make test async task , await in it:

[testclass] public class unittest1 {     [testmethod]     public async task testmongoinsertionasync()     {         mongoclass mc = new mongoclass();         await mc.savetomongoasync();     } } 

as side note improve test, , return value can assert on in test make sure document correctly inserted.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -