Vladimir Kruglov
Vladimir Kruglov

Reputation: 298

how to test WPF forms?

I'm writing complex application where are server and client application, if i want to add some new window in my application i need to run server, then run client, then login click few buttons before i reach window which i right now developing, it's annoying and takes much time. So now i'm looking for some solution where i can run test which can run only my wpf form using mock for services (so i don't need running server and don't need to login), can click or fire events and check controls appear and act the way i want. In this case i can save a lot of time because i dont need to waste time when i try to reach my form by running whole application where i need to use login, search bypass some validation forms and etc. If there is already simple solution then it will be great.

Upvotes: 2

Views: 446

Answers (2)

k.m
k.m

Reputation: 31454

Are you using any kind of dependency injection? It would be easy if so, you could simply create fake version of your heavy service using mocking framework with combination of #if-else directives. Somewhere in your application startup code:

ILoginService service;

#if DEBUG
    service = A.Fake<ILoginService>();
    // you could even set up your fakes to return logged user to
    // automate logging in process:
    var userFake = A.Fake<IUser>();
    A.CallTo(() => service.LogIn(A<string>.Ignored)).Returns(userFake);
#else
    service = new RealLoginService();
#endif

var myWindow = new MyWindow();
var viewModel = new ViewModel(service);
myWindow.DataContext = viewModel;

// ...

All you need is ViewModel (assuming you use such) being able to take ILoginService dependency. Then you can mock/fake it up to your liking.

Upvotes: 0

CJBrew
CJBrew

Reputation: 2787

Take a look at the MVVM pattern.

MVVM for WPF

Upvotes: 4

Related Questions