ฉันจะทดสอบว่า JavaScript บางตัวถูกเรียกด้วยอาร์กิวเมนต์ที่ถูกต้องใน GWT ได้อย่างไร

ฉันได้สร้าง GWT Wrapper แบบบางรอบๆ JavaScript API ที่มีอยู่ JavaScript API ได้รับการทดสอบอย่างอิสระ ดังนั้นสิ่งที่ฉันต้องทำคือทดสอบว่า GWT Wrapper เรียกใช้ฟังก์ชัน JavaScript ที่ถูกต้องพร้อมกับอาร์กิวเมนต์ที่ถูกต้อง มีความคิดเห็นเกี่ยวกับวิธีการทำเช่นนี้หรือไม่?

ปัจจุบัน GWT API มีเมธอดสาธารณะมากมาย ซึ่งหลังจากการประมวลผลเล็กน้อยจะเรียกเมธอดเนทิฟส่วนตัวซึ่งทำการเรียก JavaScript API

คำแนะนำใด ๆ ชื่นชมขอบคุณ


person Supertux    schedule 21.09.2009    source แหล่งที่มา


คำตอบ (2)


ในโลกของ Java สิ่งที่คุณขอมักจะทำโดยใช้การมอบหมายและอินเทอร์เฟซ

ฉันจะสร้างอินเทอร์เฟซ (java) ที่สอดคล้องกับ API แบบหนึ่งต่อหนึ่งที่ไลบรารี js จากนั้นสร้างการใช้งานอินเทอร์เฟซนั้นอย่างง่าย

จากนั้นโค้ด wrapper ของคุณจะล้อมอินเทอร์เฟซแทน ในระหว่างการทดสอบ คุณจะแทนที่การใช้งานอินเทอร์เฟซนั้นด้วยของคุณเอง โดยที่แต่ละวิธีจะยืนยันว่าถูกเรียกหรือไม่

E.g.

custom.lib.js has these exported methods/objects:
var exports = { 
   method1: function(i) {...}, 
   method2: function() {...},
   ...etc
}

your custom interface:
public interface CustomLib {
   String method1(int i);
   void method2();
   //...etc etc
}

your simple impl of CustomLib:
public class CustomLibImpl implements CustomLib {
   public CustomLibImpl() {
      initJS();
   }
   private native void initJS()/*-{ 
      //...init the custom lib here, e.g.
      $wnd.YOUR_NAME_SPACE.customlib = CUSTOMLIB.newObject("blah", 123, "fake");
   }-*/;
   public native String method1(int i)/*-{
      return $wnd.YOUR_NAME_SPACE.customlib.method1(i);
   }-*/;
   void method2()/*-{
      $wnd.YOUR_NAME_SPACE.customlib.method2();
   }-*/;
   //...etc etc
}

then in your Wrapper class:
public class Wrapper {
   private CustomLib customLib;
   public Wrapper(CustomLib  customLib ) {
      this.customLib = customLib;
   }

   public String yourAPIMethod1(int i) {
      return customLib.method1(i);
   }
   ///etc for method2()
}


your test:
public class YourCustomWrapperTest {
   Wrapper wrapper;
   public void setup() {
      wrapper = new Wrapper(new CustomLib() {
         //a new impl that just do asserts, no jsni, no logic.
         public String method1(int i) {assertCalledWith(i);}
         public void method2() {assertNeverCalledTwice();}
         //etc with other methods
      });
   }
   public void testSomething() { wrapper.yourAPIMethod1(1);}
}
person Chii    schedule 21.09.2009

วิธีที่ดีที่สุดในการตรวจสอบว่าฟังก์ชันเริ่มทำงานหรือไม่คือให้ฟังก์ชันเขียนถึงการปิดแล้วทดสอบหาค่าของการปิด เนื่องจากการปิดเป็นตัวแปร คุณสามารถกำหนดให้เป็นอาร์กิวเมนต์ของฟังก์ชันได้ แต่คำจำกัดความนั้นจะต้องเกิดขึ้นกับฟังก์ชันพาเรนต์

person Community    schedule 21.09.2009