0Pricing
Blockchain Smart Contracts with Solidity · درس

كتابة اختبارات العقود وتشغيلها

اكتب اختبارات آلية لعقودك الذكية لتتمكن من اكتشاف الأخطاء قبل النشر على شبكة حية.

كتابة اختبارات العقود وتشغيلها درس مجاني في Blockchain Smart Contracts with Solidity على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Blockchain Smart Contracts with Solidity، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Test Smart Contracts?

Deployed contract code is immutable and often holds real value. A bug can be unfixable and costly. Automated tests let you verify behavior repeatedly and safely before any deployment.

The Test Toolkit

Hardhat and Truffle integrate test runners. Hardhat commonly uses Mocha with the Chai assertion library and ethers.js to deploy and call contracts in tests.

Test File Layout

Tests live in the test/ folder. Each file describes a contract with describe blocks and individual cases with it blocks.

describe("Counter", function () {
  it("starts at zero", async function () {
    // ...
  });
});

Deploying in a Test

Before each test you deploy a fresh contract instance so tests stay isolated and do not affect each other.

const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();

Making Assertions

Call a contract function and assert the result with Chai. Reading the returned value confirms the contract behaves as expected.

expect(await counter.value()).to.equal(0);
await counter.increment();
expect(await counter.value()).to.equal(1);

Testing Reverts

Good tests also confirm failures. Assert that a call reverts with the expected reason when given invalid input.

await expect(counter.setOwner(addr)).to.be.revertedWith("Not owner");

Testing Events

Verify that a function emits the right event with the right arguments. Events are how off-chain apps learn about state changes.

await expect(counter.increment())
  .to.emit(counter, "Incremented")
  .withArgs(1);

Working with Multiple Accounts

Test access control by calling functions from different signers. Connect a contract to another account to simulate a non-owner trying a restricted action.

const [owner, other] = await ethers.getSigners();
await expect(counter.connect(other).reset())
  .to.be.reverted;

Manipulating Time and State

Test runners can fast-forward block time and mine blocks, letting you test time-locked logic without waiting. This is essential for vesting or auction contracts.

Measuring Coverage

A coverage tool reports which lines and branches your tests exercise. Aim for high coverage on critical paths, especially money-moving functions and access checks.

npx hardhat coverage

Running the Suite

Run all tests with a single command. Integrate this into CI so every change is verified automatically before merge or deploy.

npx hardhat test

Quick Check

Check your contract testing knowledge.

Recap

You learned to test contracts:

  • Use Mocha/Chai with ethers in the test/ folder
  • Deploy a fresh instance and assert return values
  • Test reverts, events, and multiple accounts
  • Manipulate time, measure coverage, and run in CI

A solid test suite is your best defense against costly on-chain bugs.

الأسئلة الشائعة

هل درس «كتابة اختبارات العقود وتشغيلها» مجاني؟

نعم — نص درس «كتابة اختبارات العقود وتشغيلها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Blockchain Smart Contracts with Solidity، انتقل إلى CoddyKit PRO. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.

ماذا ستتعلم في «كتابة اختبارات العقود وتشغيلها»؟

اكتب اختبارات آلية لعقودك الذكية لتتمكن من اكتشاف الأخطاء قبل النشر على شبكة حية. تتمرن على Blockchain Smart Contracts with Solidity مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Blockchain Smart Contracts with Solidity؟

لا تُشترط خبرة سابقة. Blockchain Smart Contracts with Solidity على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «كتابة اختبارات العقود وتشغيلها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Blockchain Smart Contracts with Solidity هذا؟

نعم. كل درس في Blockchain Smart Contracts with Solidity يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إعداد Hardhat أو Truffle
  2. ترجمة العقود ونشرها
  3. التفاعل مع العقود المنشورة
  4. كتابة اختبارات العقود وتشغيلها
← العودة إلى Blockchain Smart Contracts with Solidity