c# - Why can't Microsoft.VisualStudio.TestTools.UnitTesting get the DbSet<T> returned from database -


  1. project:web api
  2. run path: open testexplorer , right click item want test,and left click debug selected tests
  3. problem description: traced sql server sql server profiler, , sql server received requesting , returned items. tdd project nothing. below relevant code:

// test project test [testmethod] public void getcategorytrees() {     //arrange     categorytreescontroller controller = new categorytreescontroller();     //act     list<categorytree> result = controller.getcategorytrees();     //assert     assert.arenotequal(0, result.count); }   // real project code private applicationdbcontext db = new applicationdbcontext();  // get: api/categorytrees public list<categorytree> getcategorytrees() {     db.categorytrees.load();     return db.categorytrees.tolist(); } 

when visit api/categorytrees in ie explorer, can items in immediate window. , there 14 items. when ran test project in previous mentioned run path, got nothing. please me, thank you.

//below code coded in immediate window under test mode db.categorytrees.tolist() count = 0 db.categorytrees.load() expression has been evaluated , has no value 

// below code coded immediate window under visit ie mode

 db.categorytrees.tolist() count = 14     [0]: {models.categorytree}     [1]: {models.categorytree}     [2]: {models.categorytree}     [3]: {models.categorytree}     [4]: {models.categorytree}     [5]: {models.categorytree}     [6]: {models.categorytree}     [7]: {models.categorytree}     [8]: {models.categorytree}     [9]: {models.categorytree}     [10]: {models.categorytree}     [11]: {models.categorytree}     [12]: {models.categorytree}     [13]: {models.categorytree} 

  • according discretion, think on wrong way. regardless of test code run or not, not core of testability code design ,i raised test code complexity, isolate code, , make testable.

  • thank @nkosi commenting this


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -