Testy jednostkowe: testowanie zdarzeń

Ostatni natknąłem się na problem testowania zdarzeń. Sprawa niestety nie jest prosta na pierwszy rzut oka. Zdarzenia w aplikacji wywołują się asynchronicznie (nie wiemy dokładnie kiedy zdarzenie zajdzie) więc aby je przetestować musimy poczekać aż dane zdarzenie wystąpi i wtedy zweryfikować poprawność danych. Tak więc nasuwa się pytanie jak wstrzymać metodę testową aby czekała do momentu zajścia zdarzenia lub jeśli jest to wymagane zasygnalizowała nam, że czas na wykonanie zadania minął.

Bardzo przydatna w takich przypadkach okazuje się klasa Monitor i instrukcja lock. Cały mechanizm opiera się na zablokowaniu obiektu w metodzie testującej za pomocą metody Monitor.Wait i czekanie na odblokowanie go przez zdarzenie metoda Monitor.Pulse. Taka funkcjonalność w zupełności wystarcza na potrzeby testów jednostkowy, a prostota sprawia, że testy tworzymy w szybki sposób. Przyjdźmy do przykładu i zobaczmy jak to wszystko wygląda w praktyce.


public void MyExampleEventTest()
MyApplication target = new MyApplication();
bool result = false;
target.TestEvent += (s, e) =>
//Do something
result = s is MyApplication
lock (this)
int timeout = 1000;
lock (this)
if (!Monitor.Wait(this,timeout)) //Wait for Event
Assert.Fail("Time out");



W przedstawionym przykładzie wykorzystałem funkcję Wait która po określonym czasie kończy działanie. Oczywiście możemy użyć też innej wersji funkcji Wait np. bez podawania czasu, dzięki czemu metoda testujące będzie czekać do skutku czyli do momentu wystąpienia zdarzenia.

598 thoughts on “Testy jednostkowe: testowanie zdarzeń”

  1. You can definitely see your expertise within the work you write. The sector hopes for more passionate writers like you who are not afraid to mention how they believe. Always go after your heart.

  2. hey there and thank you for your information – I have certainly picked up something
    new from right here. I did however expertise several technical points using this site, since I experienced to
    reload the site many times previous to I could get it to load correctly.
    I had been wondering if your hosting is OK? Not that
    I am complaining, but sluggish loading instances times will often affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords.

    Anyway I am adding this RSS to my e-mail and can look out for much more of your
    respective exciting content. Make sure you update this again soon.

  3. I am just writing to make you be aware of what a superb encounter my friend’s princess found reading your site. She picked up such a lot of details, most notably what it’s like to possess an incredible coaching nature to make other people just grasp chosen specialized matters. You really did more than my expected results. Thanks for delivering those good, safe, edifying and even easy guidance on the topic to Lizeth.

  4. As I web-site possessor I believe the content matter here is rattling fantastic , appreciate it for your hard work. You should keep it up forever! Best of luck.

  5. Someone necessarily assist to make critically articles I would state. This is the very first time I frequented your website page and thus far? I amazed with the analysis you made to create this particular put up incredible. Great process!

  6. Thank you for any other wonderful post. The place else may anybody get that type of information in such an ideal way of writing? I’ve a presentation next week, and I am at the look for such info.

  7. A fascinating discussion is definitely worth comment.
    I do think that you should publish more on this subject, it might not be a taboo subject
    but usually folks don’t speak about such subjects. To the next!
    Many thanks!!

  8. You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complex and very broad for me. I’m looking forward for your next post, I will try to get the hang of it!

  9. I get pleasure from, result in I found just what I used to be taking a look for. You have ended my four day lengthy hunt! God Bless you man. Have a great day. Bye

  10. I discovered your weblog web site on google and verify a couple of of your early posts. Proceed to maintain up the very good operate. I simply extra up your RSS feed to my MSN Information Reader. Searching for forward to studying more from you afterward!?

  11. Wow, marvelous weblog layout! How lengthy have you been blogging for? you made blogging glance easy. The overall look of your website is fantastic, as well as the content!

  12. Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I acquire actually enjoyed account your blog posts. Anyway I’ll be subscribing to your feeds and even I achievement you access consistently quickly.

  13. hello there and thank you for your info – I’ve certainly picked up anything new
    from right here. I did however expertise some technical issues using
    this site, since I experienced to reload the site lots of times previous to I could get it to load correctly.
    I had been wondering if your hosting is OK? Not that I’m complaining, but slow loading instances times
    will often affect your placement in google and can damage your high-quality score if ads and marketing with Adwords.
    Well I am adding this RSS to my email and can look out for much more of your respective intriguing content.
    Ensure that you update this again soon.

  14. Hey, you used to write fantastic, but the last several posts have been kinda boring¡K I miss your tremendous writings. Past several posts are just a little out of track! come on!

  15. I think other site proprietors should take this website as an model, very clean and great user genial style and design, let alone the content. You’re an expert in this topic!

  16. What i do not understood is actually how you are not really a lot more well-favored than you may be right now. You are so intelligent. You recognize therefore considerably on the subject of this matter, produced me in my opinion believe it from so many various angles. Its like men and women are not involved until it is one thing to do with Girl gaga! Your own stuffs excellent. At all times maintain it up!

  17. I as well as my guys were actually digesting the good ideas from your web site then at once I had a terrible feeling I never expressed respect to you for those secrets. The men ended up for that reason warmed to read them and have really been enjoying them. Appreciation for actually being quite kind as well as for making a decision on certain amazing subject matter most people are really wanting to be informed on. My sincere regret for not expressing gratitude to you earlier.

  18. I’m just writing to let you understand of the notable discovery my cousin’s child enjoyed reading your web page. She came to find so many things, with the inclusion of how it is like to possess a wonderful teaching spirit to let other individuals without problems thoroughly grasp some extremely tough subject matter. You really did more than readers’ expectations. Thank you for churning out these important, healthy, revealing and also fun tips on the topic to Ethel.

  19. Thank you for sharing excellent informations. Your web-site is so cool. I’m impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found simply the information I already searched everywhere and simply could not come across. What a great site.

  20. Magnificent beat ! I would like to apprentice at the
    same time as you amend your site, how could i subscribe for a weblog site?

    The account aided me a appropriate deal. I were tiny
    bit familiar of this your broadcast provided vibrant transparent concept

  21. Thank you for each of your work on this blog. My niece takes pleasure in conducting investigation and it’s easy to understand why. We all learn all regarding the compelling method you offer worthwhile tricks through your website and as well as welcome contribution from people on the area of interest and our favorite child is truly learning a great deal. Take advantage of the rest of the new year. You have been carrying out a powerful job.

  22. It’s a shame you don’t have a donate button! I’d certainly donate to this brilliant blog!
    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google
    account. I look forward to brand new updates and will talk about this site with my Facebook group.
    Talk soon!

  23. The troublesome half is definitely defining the phrase ‘impressions.’ The
    one time you earn cash on YouTuube is when somebody interacts along with your
    ad or when it leaves ann ‘impression’ on them.

  24. Hey there, I think your blog might be having browser compatibility issues.

    When I look at your blog in Ie, it looks fine but when opening in Internet Explorer, it has some
    overlapping. I just wanted to give you a quick
    heads up! Other then that, fantastic blog!

  25. What’s Happening i am new to this, I stumbled upon this I’ve discovered It absolutely helpful and it has
    helped me out loads. I hope to contribute & aid different customers like its helped me.
    Good job.

  26. Nice post. I was checking constantly this weblog and
    I am impressed! Very helpful info specially the remaining phase 🙂 I handle such information much.

    I used to be seeking this particular info for a long time.
    Thanks and good luck.

Leave a Reply