chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi @all,
first off all I'm new in Typemock and I'm still trying out something.

I have the Following RepoManager

public class RepositoryManager
    {
        public DatabaseEntities _db = new DatabaseEntities();
      /// <exception cref="System.ArgumentNullException">source oder selector ist null.</exception>
        public string getData(string name)
        {
            return _db.DataBaseTable.Where(f => f.NAME.Equals(name)).Select(f => f.NAME).FirstOrDefault();
        }
}



And my Test.

        [Isolated]
        [TestMethod()]
        public void DatenholenTesttest()
        {
            var target = Isolate.Fake.Instance<RepositoryManager>();
            var fakeEntities = Isolate.Fake.Instance<DatabaseEntities>();
            target._db = fakeEntities;
            Isolate.WhenCalled(() => target._db.DataBaseTables).WillReturnCollectionValuesOf(_inMemoryObjects.AsQueryable());
            var  actual = target.getData("test1");
            var expected = "test1";
            Assert.AreEqual(expected, actual);
          
        }


Actual is still Empty. Can any body tell me what's wrong with my Testcode?

kind regards
asked by rednose84 (640 points)

2 Answers

0 votes
Hi,

You didn't mention what it is you are trying to do so in case I got you wrong, correct me.

I've made some assumptions in order to fill in the missing pieces:

1) DataBaseTable and DataBaseTables is the same class.
2) _inMemoryObjects.AsQueryable() is a Queryable collection set to have "test1" in it.

In this case what I would do is:


[TestMethod() , Isolated]
public void DatenholenTesttest()
{
    var fakeEntities = Isolate.Fake.Instance<DatabaseEntities>();
    Isolate.WhenCalled(() => fakeEntities.DataBaseTables).WillReturnCollectionValuesOf(_inMemoryObjects.AsQueryable());
    Isolate.Swap.NextInstance<DatabaseEntities>().With(fakeEntities);
  
    var target = new RepositoryManager();
    var actual = target.GetData("test1");

    var expected = "test1";
    Assert.AreEqual(expected, actual);
}


We use Swap.NextInstance() in order to assign fakeEntities to _db (when the _db is created using new), that's why we created fakeEntities and set the behavior of fakeEntities.DataBaseTables to return a queryable collection.

When _db.DataBaseTable.Where(..) called inside target.GetData(..). is the same as writing
_inMemoryObjects.AsQueryable().Where(f => f.NAME.Equals(name)).Select(f => f.NAME).FirstOrDefault();

If you have more question try to send me all the relevant details.
Please let me know if that helps.
answered by alex (17k points)
0 votes
Wow, that it is.. Perfect.. your assumptions are right.. and Swap was the right way...

Thanks a lot.
answered by rednose84 (640 points)
...