/// The context. ///" /> /// The context. ///" /> /// The context. ///"/>

typemock test not working

193 views Asked by At
public class GetDatasourceDependencies : BaseProcessor
    {
        /// <summary>
        /// The process.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        public override void Process(GetDependenciesArgs context)
        {
            Assert.IsNotNull(context.IndexedItem, "indexed item");
            Assert.IsNotNull(context.Dependencies, "dependencies");

            Item item = (context.IndexedItem as SitecoreIndexableItem);

            if (item != null)
            {
                var layoutLinks = Globals.LinkDatabase.GetReferrers(item, FieldIDs.LayoutField);

                var sourceUris = layoutLinks.Select(l => l.GetSourceItem().Uri).Where(uri => uri != null && uri != item.Uri).Distinct();   
                context.Dependencies.AddRange(sourceUris.Select(x => (SitecoreItemUniqueId)x));

            }
        }
    }

How do I write a test with typock for this. I am very new to typemock and have written something like this. I understand that i need to mock the args and context but as the method is returning nothing back, how do i test it. My test should be success only if the context.dependents have some values.

 [Test]
      public void GetIndexingDependencies_Calls()
      {
        var indexable = Isolate.Fake.Instance<IIndexable>();
        var fake = Isolate.Fake.Instance<GetDependenciesArgs>();            
        var context = Isolate.Fake.Instance<GetDatasourceDependencies>();          
        var obj = new GetDatasourceDependencies();
        Isolate.Verify.WasCalledWithAnyArguments(() => context.Process(fake));
        Isolate.WhenCalled(() => fake.IndexedItem).WillReturn(indexable);




        //Isolate.WhenCalled(() => fake.Dependencies.Count).WillReturn(2);


      }
1

There are 1 answers

1
Al.exe On BEST ANSWER

Disclaimer, I work at Typemock.

You can use a real collection for context.Dependencies and assert that some items are actually added.

To achieve this you should replace the collection that Globals returns and make sure that linq can process it as you expect (I just returned the same collection from the linq query for the sake of the example).

Your test should look something like this:

    public void GetIndexingDependencies_Calls()
    {
        //Arrange
        var fakeContext = Isolate.Fake.Instance<GetDependenciesArgs>();
        fakeContext.Dependencies = new List<SitecoreItemUniqueId>();

        var itemList = new List<SomeItem> { new SomeItem(), new SomeItem() };
        Isolate.WhenCalled(() => Globals.LinkDatabase.GetReferrers(null, null)).WillReturn(itemList);
        Isolate.WhenCalled(() => itemList.Select(l => l.GetSourceItem().Uri).Where(uri => true).Distinct()).WillReturn(itemList);

        //ACT
        var underTest = new GetDatasourceDependencies();
        underTest.Process(fakeContext);

        //ASSERT
        Assert.AreEqual(2, fakeContext.Dependencies.Count);
    }

Some more points:

  • Don't fake whatever you're testing, in this case it's GetDatasourceDependencies. If it's faked it will not really be called and not be tested.
  • Don't write asserts\verify before the executing the code that you're trying to test, in this case context.Process(fake) wasn't called before verify.