0

When writing Unit Tests for a function that is consuming a List<Microsoft.Bing.Speech.RecognitionPhrase> I face the following error:

Invalid setup on a non-virtual (overridable in VB) member: x => x.Confidence

After reading here, I get that this is because the property is not virtual. I have been reading in the site about interfaces, wrappers, virtuals...but with no success.

I have access to RecognitionPhrase [from metadata] and it has public Confidence Confidence { get; } so there's no set here. I have tried to create a public interface IRecognitionPhrase and a public class RecognitionPhrase : IRecognitionPhrase, but then in the final casting it says that it cannot cast my RecognitionPhrase to Microsoft.Bing.Speech.RecognitionPhrase.

I have read something about reflection but it seems to work with private setters rather than with no setters.

I'm out of ideas now. Any directions are much appreciated (and of course if someone has already mocked List<Microsoft.Bing.Speech.RecognitionPhrase> please comment how did you do it) Thanks

I'm open to employing any other testing framework.

malarres
  • 2,941
  • 1
  • 21
  • 35
  • Include a [mcve] that can be used to reproduce the problem. – Nkosi Apr 17 '18 at 16:31
  • @Nkosi I'm afraid I wasn't disciplinated enough to having commented everything that I tried and didn't work...I just replaced one code with the other. – malarres Apr 19 '18 at 10:31

1 Answers1

0

I've finally solved it using reflection...but not reflection of the Mock (which was throwing an exception)

        //var mockFrase = new Mock<RecognitionPhrase>();
        //PropertyInfo propertyInfo = mockFrase.GetType().GetProperty("Confidence");
        //propertyInfo.SetValue(mockFrase, Confidence.High);

Instead, using reflection on the real object solved the problem for me:

        var frase = new RecognitionPhrase();
        PropertyInfo propertyInfo = frase.GetType().GetProperty("Confidence");
        propertyInfo.SetValue(frase, Confidence.High);
malarres
  • 2,941
  • 1
  • 21
  • 35