Thursday 15 August 2013

c# - Capturing Dialog-Windows with SpecFlow -



c# - Capturing Dialog-Windows with SpecFlow -

i new testing still, have been using specflow a few months. not exclusively sure if going inquire possible, maybe have suggestion go problem.

synopsis: feature file makes phone call method creates dialog window stored in variable created in method. user need fill out dialog window (it picking file, , clicking ok). rest of method relies on info provided dialog window.

problem: since window created in method , result stored in variable created @ moment, can not provide info variable. in order behavior tests finish, need provide information.

example code:

feature file:

given initialize class , click on alter selected item

steps file:

[given(@"i initialize class")] public void giveniinitializetheclass() { dostuff(); someclass testclass = new someclasee(); } [given(@"iclickonchangeselecteditem")] public void giveniclickonchangeselectitem() { testclass.changeitem(); }

method class:

public void changeitem() { var window = new somedialogwindow(); var result = window.showdialog(); if (result.hasvalue && result.value) { newitem = window.selecteditem; } }

i know how go if alter method in class, but, in example, can create no changes class itself. 1 time again not know if possible assign result, or command window since variables both created within method.

depending on want quite mutual pattern , easy solve, first lets consider kind of testing might want running.

unit tests - in unit test wants test implementation of someclass don't care implementation of other classes including somedialogwindow. alternatively writing tests care solely implementation of somedialogwindow, or implementation of someclass::changeitem , nil else. how fine want go? these tests there pinpoint of code broken.

acceptance tests - build these test how works together. care interaction between different units , such show things unit tests don't find. subtle configuration issues, or more complicated interactions between units. unfortunately cover huge swathes of code, leave needing more precise find out wrong.

in test driven development project, might write single acceptance test can see when done, write many unit tests 1 @ time, each unit test used add together little piece of functionality codebase, , confirm works before moving next.

now how it. long able modify someclass not huge change, in fact need add together virtual changeitem method make

public virtual void changeitem() ...

what allows replace method different implementation when testing it. in simplest form can declare like,

namespace xxx.tests { public class testablesomeclass : someclass { public item testitem {get;set;} public override void changeitem() { newitem = testitem; } } }

this mutual pattern , known stub. we've reduced functionality in changeitem downwards bare essentials minimal stub of original intent. aren't testing changeitem anymore, other parts of our code.

in fact pattern pattern mutual there libraries there help mock function instead. tend utilize 1 called moq , this.

//given var desireditem = ... mock<someclass> mymock = new mock<someclass>(); mymock.setup(x=>x.changeitem).returns(desireditem); var testclass = mymock.object; //when testclass.changeitem(); //then testclass.newitem.shouldequal(....);

you notice in both these examples have gotten rid of gui part of codebase can concentrate on functionality. recommend approach getting 90% codebase covered , end rapid uncomplicated testing. need acceptance tests test ui, , come altogether more complicated beast.

for eaxmple, ui include blocking calls when displays visual elements, such somedialogwindow.showdialog() , these have occur on commonly referred ui thread. fortunately while 1 thread can ui thread, thread can ui thread if gets there first, need have @ to the lowest degree 1 thread displaying ui , running tests. can steal pattern web based testing , create driver classes command ui, , these end on test running thread performing click operations , polling see if operations complete.

if need go these lengths don't start larn how utilize testing frameworks, start simple stuff.

c# specflow

No comments:

Post a Comment