简体   繁体   中英

In C# how can I automate testing of Word find/replace code?

I have an application in C# that automates the filling out of forms in Word from a Database. I'm currently adding unit-testing to my application. there are a lot of calls to Replace(...) , which is a "simple" wrapper for Range.Find.Execute(..) . I have several other, more complex methods, that are just fancy wrappers to Replace(...) I have most of them in the same class WordWrapper which also handles the allocation and de-allocation of the WordApplication object and the open Document list. What is a good method for Automating the testing of something like that?

I especially want to make sure my Allocation and de-allocation are properly handled, and that I get appropriate Error Messages/Exceptions when things go wrong talking to Word.

UPDATE: The reason I'm worried about the Allocation/De-Allocation is that, Word is an expensive object to create/destroy, so I've made it static so I can re-use it between invocations of the form window that actually runs word.

I would highly recommend that this NOT be an automated testing function.

I've worked on many such systems and quite frankly there are things that occur in text replacement that an automated solution just won't find. You are much better off drafting a number of colleagues in the affected business unit to QA the updates.

One example: Let's say the form has enough space for 6 characters. You inject 10. The find / replace code obviously worked. However, because the amount of characters ran over, it completely jacked the structure of the page and what was a 1 pager now has 2 pages...which will probably not be acceptable.

UPDATE: I'm going to leave this answer simply because it might help someone in the future stumbling through the concept of find/replacing text in documents. However, after rereading the question (along with its update), I don't think this answer really applies at all.

If you need to test that you handle errors and exception from Word correctly then your best bet it to create a mock interface for your tests.

This looks like the real interface, but you can control what each call will return so, for example, if you pass "Find xyz" it will return "true", but if you pass "Find 123" it will raise an exception. You can then verify that your code handles this correctly.

I don't know of any mocking framework that explicitly targets Word however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM