Kur çevirme testleri
Test · Zorluk 3/5 · TypeScript · 39 satırKöken: Kurgu
Görev
Yapay zekâdan istenen
convertPrice(amount, from, to, rates) için birim testleri yaz. Fonksiyon kuru rates.rate(from, to) ile alır (bir birim from kaç birim to eder), tutarı çevirir ve kuruşa yuvarlar. Para birimi aynıysa kur sormadan tutarı döndürür; kur alınamazsa hata yukarı iletilir. Kur servisi testte sahte nesneyle verilir.
Teslim notu
Yapay zekâ kodu teslim ederken şunu yazdı:
“Çevirme, kuruşa yuvarlama, aynı para birimi ve kur servisinin hatası test edildi. Kur servisi sahte nesneyle verildiği için testler ağa çıkmıyor.”
Yapay zekânın yazdığı kod
TypeScript39 satırHatalı bulduğun satırları işaretle; her işarete önem seç ve nedenini bir cümleyle yaz.
Satıra tıkla ya da dokun. Klavyede satıra gidip Enter'a bas.
Önem nasıl seçilir
- KritikVeri kaybı, güvenlik açığı ya da para kaybı
- ÖnemliGörevin istediği bir durumda kullanıcı yanlış sonuç alır
- KüçükGörevin dışında kalan nadir bir uç durum ya da okunurluk
Test konusunda önem, testin yakalamadığı hatalı uygulamanın etkisine göre belirlenir.
Gerekçe puanlanmaz; sonuçta örnek cevapla karşılaştırırsın.
import assert from "node:assert/strict";import type * as Pricing from "./subject.ts";const rates: Pricing.RateSource = { rate: async () => 1.25,};export async function testConvertsWithRate({ convertPrice }: typeof Pricing) { assert.equal(await convertPrice(10, "EUR", "USD", rates), 12.5);}export async function testRoundsToCents({ convertPrice }: typeof Pricing) { assert.equal(await convertPrice(0.99, "EUR", "USD", rates), 1.24); assert.equal(await convertPrice(10.01, "EUR", "USD", rates), 12.51);}export async function testSameCurrency({ convertPrice }: typeof Pricing) { let lookups = 0; const counting: Pricing.RateSource = { rate: async () => { lookups += 1; return 2; }, }; assert.equal(await convertPrice(10, "EUR", "EUR", counting), 10); assert.equal(lookups, 0);}export async function testRateFailure({ convertPrice }: typeof Pricing) { const failing: Pricing.RateSource = { rate: async () => { throw new Error("rate service down"); }, }; await assert.rejects( convertPrice(10, "EUR", "USD", failing), /rate service down/, );}Kanıtlayan test
Doğru seçenek, bu kodda başarısız olan, yani hatayı ortaya çıkaran testtir.
Gönderince sayaca yedi bilgi gider; gerekçe metnin cihazında kalır. Veriler