1

I am new to moq and trying to implement moq test for the below class but got stuck with a error.

public class ClassToTest {
   internal Func<string> GetConfigString = 
        () => ConfigurationManager.AppSettings["somekey"].ToString()
}

Test Method

var mock = new Mock<ClassToTest>();
mock.Setup(m => m.GetConfigString).Returns(It.IsAny<Func<string>>());

The setup fails with a message:

expression is not a method invocation

I have added the below line in AssemblyInfo.cs but still the test fails. can someone let me know where i am wrong?

[assembly:InternalsVisibleTo("DynamicProxyGenAssembly2,Publi‌​cKey=002400000480000‌​09400000006020000002‌​40000525341310004000‌​001000100c547cac37ab‌​d99c8db225ef2f6c8a36‌​02f3b3606cc9891605d0‌​2baa56104f4cfc0734aa‌​39b93bf7852f7d926665‌​4753cc297e7d2edfe0ba‌​c1cdcf9f717241550e0a‌​7b191195b7667bb4f64b‌​cb8e2121380fd1d9d46a‌​d2d92d2d15605093924c‌​ceaf74c4861eff62abf6‌​9b9291ed0a340e113be1‌​1e6a7d3113e92484cf70‌​45cc7")]
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51
Manoj
  • 13
  • 6

1 Answers1

0

Convert GetConfigString into e.g. property and add virtual keyword otherwise it won't be possible to use Mock with it.

    internal virtual Func<string> GetConfigString
    {
        get
        {
            return () => ConfigurationManager.AppSettings["somekey"];
        }
    }
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51