سؤال

I want to assert that a call on my real object (system under test) was called. Here is my test

// Arrange
var contextFactory = A.Fake<IContextFactory>();
var db = A.Fake<IDatabase>();
A.CallTo(() => contextFactory.GetContext()).Returns(db);
var vm = new MainViewModel(contextFactory);

// Act
vm.Loaded();

// Assert
A.CallTo(() => vm.LoadModels(db)).MustHaveHappened();

I'm getting an ArgumentException that says "The specified object is not recognized as a fake object." How do I test that the LoadModels() method in my MainViewModel gets called from the Loaded() method?

EDIT

The reason I'm doing it this way is because the Loaded() method calls a bunch of other methods when the app starts and I don't want to have to setup all the other stuff for each test, I just want to make sure that all the proper methods get called and then test them individually. I'm open to suggestion for a better way of going about this.

Here are the Loaded and LoadModels methods

internal virtual void Loaded()
{
    using (var db = _contextFactory.GetContext())
    {
        LoadModels(db);
        // bunch of other method calls
    }
}

internal virtual void LoadModels(IDatabase db)
{
    Models = new ObservableCollection<Model>(db.Models);
}
هل كانت مفيدة؟

المحلول 2

I ended up moving the functionality of LoadModels() to another class that implements IStartupDataLoader and then I was able to test it like this

// Arrange    
var sdl = A.Fake<IStartupDataLoader>();
var exp = A.Fake<ObservableCollection<Model>>();
A.CallTo(() => sdl.LoadModels()).Returns(exp);
var sut = new MainViewModel(sdl);

// Act
sut.Loaded();

// Assert
Assert.That(exp == sut.Models);

نصائح أخرى

It looks like you're verifying a method (vm.LoadModels) that isn't part of the fake (db). You can only verify methods on a fake, not methods that happen to take the fake as an argument.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top