Fody / Ionad

Replaces static method calls.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clarify how to specify scope of replacement class.

MNF opened this issue · comments

From ReadMe is not clear, how to specify the scope of replacement. E.g. When calling from test code I want to use DateTimeSubstitute, but when calling from production code I want to use DateTime. Please clarify in documentation.
Btw, why Ionad?

You probably should only include the Nuget and replacement in your test project. In most cases I declare the replacement in my Test class unless I find myself duplicating the code.

As @MNF I also think this must be clarified.
If I create a DateTime substitute class on another namespace related (and used) on my test class, DateTime is not substituted.

If I declare it on the same namespace (but another assembly) It is not substituted either.

In example:

On this code:

  • Test a does not pass.
  • But test b is passing (green)
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FodyIonadConsoleApp;

namespace FodyIonadLibraryTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void a()
        {
            DateTimeSubstitute.Current = new DateTime(2000, 01, 01, 00, 00, 00);
            var sut = new DateComparer();
            var result = sut.Compare(new DateTime(2000, 01, 01, 00, 05, 00));
            Assert.AreEqual(5, result);
        }

        [TestMethod]
        public void b()
        {
            var result = DateTime.Now;
            Assert.AreEqual(2000, result.Year);
        }
    }
}

With this DateComparer simple class:

using System;

namespace FodyIonadConsoleApp
{
    public class DateComparer
    {
        public double Compare(DateTime dateToCompare)
        {
            return (DateTime.Now - dateToCompare).TotalMinutes;
        }
    }
}

And with the sample StaticReplacement example:

using Ionad;
using System;

namespace FodyIonadLibraryTests
{
    [StaticReplacement(typeof(DateTime))]
    public static class DateTimeSubstitute
    {
        public static DateTime Current { get; set; }

        public static DateTime Now { get { return Current; } }
    }
}

What is wrong here?

closing this as stale. if it is still an problem please raise a new issue