Validating the results of a mocked object's function?

Oct 1, 2013 at 1:31 PM
I'm sure I'm going about this the wrong way, and I tried searching around but may not be using the right terms. Any help is appreciated.

So, I'm using a library, and because it deals with the file system, I'm mocking it (and thankful it has interfaces so that I can quickly do so!)

The object I'm trying to mock, which represents an excel worksheet, has a method essentially called "IsEmpty(boolean aBooleanToSet)".

I can write this up with Moq along the lines of:
mockWorkbook.Setup(m => m.IsEmpty(It.IsAny<bool>())).Returns(true);
My validator class is pretty simple and looks like the following:
    public class InvoiceDetailsWorksheetValidator : AbstractValidator<IXLWorksheet>
        public InvoiceDetailsWorksheetValidator()
            RuleFor(x => x.IsEmpty())
                .NotEqual(true).WithMessage("Worksheet must not be empty.");
I have the following test, using
public void Validate_WithFirstWorksheetEmpty_ReturnsInvalid()
       // Get the otherwise valid worksheet
       var mockWorkbook = TestHelper.GetMockValidInvoiceDetailsWorksheet();

       // Set the one specific value to a mocked failure
        mockWorkbook.Setup(m => m.IsEmpty(It.IsAny<bool>())).Returns(true);

       // Pass the mock object to the validator
       var validationResults = new InvoiceDetailsWorksheetValidator().Validate(mockWorkbook.Object);

       // Assert that the validator finds it invalid
When I run the test, rather than it passing, I see the following error from FluentValidation:
Property name could not be automatically determined for expression x => x.IsEmpty(). Please specify either a custom property name by calling 'WithName'.
What am I doing wrong?
Oct 1, 2013 at 1:35 PM
In case folks prefer the StackOverflow environment, I've also put it up here:

Will also post back to this discussion thread if the issue is resolved there.
Oct 1, 2013 at 2:01 PM
FluentValidation expects rules to be defined on properties, but your rule is defined for a method - IsEmpty(). You'll need to explicitly specify a name (by calling OverridePropertyName as part of the chain).

Oct 1, 2013 at 2:27 PM
Perfect -- took me a second to find out that I had to add it on the end of the chain.

For those following along at home, the working validation rule was the following:
    .NotEqual(true).WithMessage("Worksheet must not be empty.").OverridePropertyName("IsEmpty");
Thanks again for this great library and being such an excellent steward of it.