سؤال

هل هناك طريقة لاختبار أبي مكتوبة باستخدام نقطة النهاية سحابة جوجل باستخدام جونيت أو إطار آخر?

في الوثائق هناك مثال باستخدام حليقة الأوامر وربما المنطق وراء ذلك هو اختبار واجهة برمجة التطبيقات من جانب العميل فقط.

عندما حاولت العثور على بعض النهج كيفية اختبار أبي من جانب الخادم ، جئت عبر إمكانية كتابة اختبارات جونيت واستدعاء هتبورلكونكتيونل لوكالهوست ، ولكن هناك مشاكل مع هذا النهج.على سبيل المثال ، يجب أن يكون مثيل محرك التطبيق قيد التشغيل بالفعل قبل الاختبار ، ولكن أنا نشر محليا مع مخضرم والاختبار هو نشر مسبق ، لذلك إذا كنت قد كسر الاختبارات فإنه لا نشر خادم ديف وأنا لا أشعر أن هذا هو الطريق الصحيح لإعادة كتابة خطوات مخضرم.

تحرير 1: وجدت شيئا مشابها لبيثون: كيفية اختبار وحدة نقاط النهاية سحابة جوجل

هل كانت مفيدة؟

المحلول

مع تشييء يمكنك أن تفعل ذلك مثل هذا.على سبيل المثال ، دعنا نعلن BooksEndpoint على النحو التالي:

@Api(
    name = "books",
    version = "v1",
    namespace = @ApiNamespace(ownerDomain = "backend.example.com", ownerName = "backend.example.com", packagePath = "")
)
public class BooksEndpoint {

    @ApiMethod(name = "saveBook")
    public void saveBook(Book book, User user) throws OAuthRequestException, IOException {
        if (user == null) {
            throw new OAuthRequestException("User is not authorized");
        }

        Account account = AccountService.create().getAccount(user);

        ofy().save()
                .entity(BookRecord.fromBook(account, book))
                .now();
    }

}

لاختباره ، ستحتاج إلى التبعيات التالية:

testCompile 'com.google.appengine:appengine-api-labs:1.9.8'
testCompile 'com.google.appengine:appengine-api-stubs:1.9.8'
testCompile 'com.google.appengine:appengine-testing:1.9.8'
testCompile 'junit:junit:4.12'

الآن ، سيبدو الاختبار كما يلي:

public class BooksEndpointTest {

    private final LocalServiceTestHelper testHelper = new LocalServiceTestHelper(
            new LocalDatastoreServiceTestConfig()
    );

    private Closeable objectifyService;

    @Before
    public void setUp() throws Exception {
        testHelper.setUp();
        objectifyService = ObjectifyService.begin(); // required if you want to use Objectify
    }

    @After
    public void tearDown() throws Exception {
        testHelper.tearDown();
        objectifyService.close();
    }

    @Test
    public void testSaveBook() throws Exception {
        // Create Endpoint and execute your method
        new BooksEndpoint().saveBook(
                Book.create("id", "name", "author"),
                new User("example@example.com", "authDomain")
        );

        // Check what was written into datastore
        BookRecord bookRecord = ofy().load().type(BookRecord.class).first().now();

        // Assert that it is correct (simplified)
        assertEquals("name", bookRecord.getName());
    }

}

ملاحظة ، أنا باستخدام BookRecord و Book هنا-تلك هي كياني وبوجو ، لا شيء خاص.

نصائح أخرى

أولا، شكرا على الرابط إلى إجابة بيثون.فيما يتعلق ب Java، هذه UDALDY CAIRD يقع حول مشروع Google Cloud Qualpoints في Java، وله كثيرأمثلة التعليمات البرمجية حول كيفية اختبار نقاط النهاية.الرمز المصدر هو هنا .لقد كنت أحاول إعادة إنتاج المشروع في بيثون، وللسوء الحظ، لا يمكن أن تقدم أي تفاصيل بناء على الخبرة الشخصية مع نقاط النهاية الاختبار المكتوبة في جافا، ولكن آمل أن تساعد الروابط!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top