1

At the moment I'm developing an UnitTest for a CustomControl. In this Control there is some Code, which is only excuted when the called method is not called in the DesignTime. When I run the test, the given Code is not executed, because the current context seems to be the DesignTime.

Example:

public void SomeMethod() // EDIT: this is the OnLoaded Event for example.
{
    if(IsNotInDesignMode()) // This is implemented somewhere else.
    {
        // I want this to be called in the Test Execution.
        DoSomething();
    }
}

EDIT: Seems like my first attempt wasnt clear enough, so here another description.

Is there a way to tell my Test Method/Class to run the Test as a Runtime Version? I dont want to change Properties or Methods in my CustomControl. And I dont have acccess to the Method IsNotInDesignMode() (by mocking it for example), because it is implemented in an external library.

EDIT 2: See Code.

Thanks for helping me out.

Febertson
  • 398
  • 4
  • 23
  • You cannot "jump out of design time". Your user control is either being displayed in a designer, or displayed at runtime. Explain what exactly you're trying to do and elaborate on what you mean by "jump out of design time". More context is required to answer this. If you mean you want the property `IsNotInDesignMode` to get a certain value while unit testing the control, then create a constructor parameter that sets that property, or directly assign it from your unit test. – CodeCaster Oct 18 '17 at 10:45
  • I understand that you might not understand my problem. But why did you mark it as duplicate? You prevent other users from opening my question and trying to give an answer. The link you provided is a complete different question. – Febertson Oct 18 '17 at 10:47
  • The problem description you give is answered by the duplicate: you can only _detect_, not _change_ design time - it's a read-only property. So if you want your real problem solved, read [ask] and [edit] your question so it can be actually answered, and I'll be happy to reopen. – CodeCaster Oct 18 '17 at 10:49
  • Does Stackoverflow expect users to read all questions related to a given word? "UnitTest" for example and read all answers of a question that does not handle my problem? Its a bit harsh of closing my question for your reasons, because in my eyes it tackles the Problem from a different side. Ive asked several questions on Stackoverflow. And its the first time that i have problems like this. This seems to me like closing question out of boredom. – Febertson Oct 18 '17 at 10:58
  • No, it is required to ask an answerable question. Your edit made it just that. – CodeCaster Oct 18 '17 at 11:08

1 Answers1

2

It appears you have business logic mixed in with view logic. There is no reason to unit test the GUI.

I recommend you encapsulate your business logic into its own class(es). Then call the specific operations from the view(s).

Once that is done, your unit test can test the business logic directly and not deal with design time.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Thanks for this first approach, but there is no Business Logic in the Method. The SomeMethod might be the LoadedEvent for example. And i cant put my stuff into the Constructor or somewhere else, because my values that I need aren't set there yet. – Febertson Oct 19 '17 at 05:00
  • @Febertson With correct MVVM you should be able to have separate projects for view and viewmodel. The unit tests shouldn't even reference the view project because all they need is the viewmodel and BLL. – grek40 Oct 19 '17 at 07:35
  • I understand what you are saying. But I have a Control which has a new Property that generates dynamic Child Elements. That's the function i want to test. Create the Control in my Test. Set the new Property and see if the Children are created correctly. This function is purly on UI side. Maybe this shouldn't or can't be tested with UnitTests. That's what im trying to evaluate at the moment. – Febertson Oct 19 '17 at 08:11
  • In my 25 years of development, I have yet to work for a company that does *automated* UI testing. Historically that has been done by manually testing the process. Maybe that new Skynet thing will change all that, but for now you describe UI manual testing. – ΩmegaMan Oct 19 '17 at 13:36
  • Yeah for me it was kinda weird too. But I tried it and wondered if it can be done "easily". But it seems that the manual way is faster and easier. – Febertson Oct 19 '17 at 14:17