mercredi 24 février 2016

Get method name of property setter in c#

I am using FakeItEasy to check that a call to the public setter method of a property has been called.

The property is called Description, and at the moment I am testing it like this:

A.CallTo(model)
    .Where(x => x.Method.Name.Equals("set_Description"))
    .WithAnyArguments()
    .MustHaveHappened();

This works functionally, however the downside of using a magic string for the method name is that if I refactor the name of the property, the test will fail and I will have to manually change the strings in all of the tests.

Ideally, I would like to know of a way to do it like in this piece of pseudocode:

var setterName = model.GetType()
                         .SelectProperty(x => x.Description)
                         .GetSetterName();

A.CallTo(model)
    .Where(x => x.Method.Name.Equals(setterName))
    .WithAnyArguments()
    .MustHaveHappened();

This way, if I right-click refactor the Description property, the tests won't need to be updated. How can I do this?





Aucun commentaire:

Enregistrer un commentaire