اختبار الوحدات والمحاكاة
اكتب اختبارات وحدات سريعة ومعزولة لمنطق الأعمال في Flutter، واستخدم mocks لاختبار التعليمات البرمجية التي تعتمد على الخدمات وواجهات API.
اختبار الوحدات والمحاكاة درس مجاني في Flutter Mobile Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flutter Mobile Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Testing Pyramid
You have seen widget and integration tests. At the base sits the fastest layer: unit tests that verify a single function or class in isolation.
- Many unit tests (fast)
- Fewer widget tests
- Even fewer integration tests (slow)
Anatomy of a Unit Test
A unit test uses test() and expect() from the test package.
import 'package:test/test.dart';
void main() {
test('adds two numbers', () {
expect(2 + 3, equals(5));
});
}Testing a Class
Instantiate the class under test and assert on its behaviour.
test('counter increments', () {
final counter = Counter();
counter.increment();
expect(counter.value, 1);
});Matchers
Matchers express expectations clearly.
equals(x)isTrue/isNullthrowsExceptioncontains(x)
expect(list, contains('apple'));
expect(() => parse('bad'), throwsFormatException);Grouping Tests
Use group() to organize related tests and share setup.
group('Cart', () {
test('starts empty', () { ... });
test('adds items', () { ... });
});setUp & tearDown
setUp runs before each test; tearDown runs after — perfect for fresh fixtures.
late Cart cart;
setUp(() => cart = Cart());
tearDown(() => cart.clear());The Problem of Dependencies
What if your class calls a real API or database? Tests would be slow and flaky. The solution is mocking — replacing dependencies with fakes you control.
Mockito Setup
Add mockito and build_runner, then annotate to generate mocks.
@GenerateMocks([ApiService])
void main() {}
// flutter pub run build_runner buildStubbing Methods
Use when(...).thenReturn(...) to define what a mock returns.
final mockApi = MockApiService();
when(mockApi.fetchUser()).thenAnswer((_) async => User('Ada'));Verifying Interactions
verify confirms a method was called the expected number of times.
await repository.loadUser();
verify(mockApi.fetchUser()).called(1);Testing Async Code
Mark the test body async and await futures, or use the completes matcher.
test('loads data', () async {
final result = await repo.load();
expect(result, isNotEmpty);
});Quick Check
Why would you use a mock in a unit test?
Recap
You learned unit testing and mocking:
- test/expect/matchers and group/setUp
- Mockito to generate mocks
- when().thenReturn and verify().called
- Testing async code
Unit tests give you fast confidence in your business logic.
الأسئلة الشائعة
هل درس «اختبار الوحدات والمحاكاة» مجاني؟
نعم — نص درس «اختبار الوحدات والمحاكاة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flutter Mobile Development، انتقل إلى CoddyKit PRO. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.
ماذا ستتعلم في «اختبار الوحدات والمحاكاة»؟
اكتب اختبارات وحدات سريعة ومعزولة لمنطق الأعمال في Flutter، واستخدم mocks لاختبار التعليمات البرمجية التي تعتمد على الخدمات وواجهات API. تتمرن على Flutter Mobile Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flutter Mobile Development؟
لا تُشترط خبرة سابقة. Flutter Mobile Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «اختبار الوحدات والمحاكاة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flutter Mobile Development هذا؟
نعم. كل درس في Flutter Mobile Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مبادئ اختبار العناصر
- اختبار التكامل
- أدوات وتقنيات تصحيح الأخطاء
- اختبار الوحدات والمحاكاة