{"id":13288201,"url":"https://github.com/anhfactor/near-101-dacade-unit-test","last_synced_at":"2025-03-10T05:32:12.843Z","repository":{"id":40485621,"uuid":"488899026","full_name":"anhfactor/near-101-dacade-unit-test","owner":"anhfactor","description":"NEAR 101 Dacade unit test contract assembly script","archived":false,"fork":false,"pushed_at":"2022-05-05T09:13:04.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-07-29T16:58:23.917Z","etag":null,"topics":["as-pect","assemblyscript","dacade","near","nearprotocol","smartcontracts","testing"],"latest_commit_sha":null,"homepage":"https://anhfactor.github.io/near-101-dacade-unit-test/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anhfactor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-05T08:58:18.000Z","updated_at":"2023-03-10T07:05:23.000Z","dependencies_parsed_at":"2022-08-09T21:51:14.216Z","dependency_job_id":null,"html_url":"https://github.com/anhfactor/near-101-dacade-unit-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhfactor%2Fnear-101-dacade-unit-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhfactor%2Fnear-101-dacade-unit-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhfactor%2Fnear-101-dacade-unit-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhfactor%2Fnear-101-dacade-unit-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anhfactor","download_url":"https://codeload.github.com/anhfactor/near-101-dacade-unit-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242798244,"owners_count":20186882,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["as-pect","assemblyscript","dacade","near","nearprotocol","smartcontracts","testing"],"created_at":"2024-07-29T16:55:00.589Z","updated_at":"2025-03-10T05:32:12.571Z","avatar_url":"https://github.com/anhfactor.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eNEAR 101 Dacade unit test contract\u003c/h1\u003e\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Project setup](#project-setup)\n- [Write a couple of tests for the contract](#write-a-couple-of-tests-for-the-contract)\n- [TLDR](#tldr)\n\n### Introduction\nAfter finish course [101 NEAR from dacade](https://dacade.org/communities/near/courses/near-101/). I realized that the course has not yet implemented unit testing for smart contracts. In blockchain, writing unit tests is very important to increase security before publishing the contract on the mainnet. \n\nThe course using assembly script for smart contract, i plan to implement unit test by using [as-pect](https://github.com/jtenner/as-pect) and the syntax resembles RSpec. The library is well [documented](https://tenner-joshua.gitbook.io/as-pect/) but sometimes the [tests for the testing library](https://github.com/jtenner/as-pect/tree/master/packages/assembly/assembly/__tests__) might be the best source of examples to help you learn quickly.\n\n### Project setup\nFirst copy the contract in folder smartcontract from [101 Near] https://github.com/dacadeorg/near-marketplace-dapp/tree/master/smartcontract and run ```yarn``` to install\n\nCreate the file as-pect.config.js to export module as-pec and add the following code:\n```\nmodule.exports = require('near-sdk-as/imports')\n```\n\nInside folder assembly create a folder __tests__. Go to __tests__ create 2 file\n- as-pect.d.ts: This file is used to define types that used in as-pect and add the following code\n```\n/// \u003creference types=\"@as-pect/assembly/types/as-pect\" /\u003e\n```\n- marketplace.spec.ts: This file create an entry for unit test\n\nThe project structure should look like this:\n```\n├── asconfig.json\n├── assembly\n|   |──__tests__\n│   |           ├── as-pect.d.ts\n│   |           ├── marketplace.spec.ts\n│   ├── as_types.d.ts\n│   ├── index.ts\n│   └── tsconfig.json\n├── package.json\n└── as-pect-config.js\n```\n\n### Write a couple of tests for the contract\nNavigate to file marketplace.spec.ts ```assembly/__tests__/marketplace.spec.ts```\n1. Import all function \u0026 model\n```\nimport { buyProduct, setProduct, getProduct, getProducts } from '../index';\nimport { productsStorage } from '../model';\n```\n\n2. Import u128 (to parse price amount) \u0026 VMContext (VMContext will help us in mock context when testing, such as attaching a deposit to a message.)\n```\nimport { u128, VMContext } from 'near-sdk-as';\n```\n\n3. Then copy these code:\n```\ndescribe(\"Test manipulate product data\", () =\u003e{\n    beforeEach(() =\u003e {\n        VMContext.setCurrent_account_id('alice.near');\n        VMContext.setSigner_account_id('alice.near');\n        VMContext.setAttached_deposit(u128.from(\"10000000000000000000000\"));\n\n        setProduct({\"id\": \"100\", \"name\" : \"BBQ\", \"description\": \"Its BBQ\", \n                    \"image\": \"https://i.imgur.com/yPreV19.png\", \"location\": \"Mexico\",\n                    \"price\": u128.from('10000000000000000000000'), \"owner\": \"dacade.testnet\", \"sold\": 0});\n    });\n    it(\"should add product\", () =\u003e {\n        log(\"setProduct\")\n        expect(productsStorage.length).toBe(\n            1,\n            \"should only contain one listed product\"\n        );\n    });\n\n    it(\"should get product\", () =\u003e {\n        const listProduct = getProducts();\n\n        log(\"getProducts\")\n        expect(listProduct[0].name).toBe(\n            \"BBQ\",\n            \"name should BBQ\"\n        );\n\n        log(\"getProduct\")\n        expect(() =\u003e {\n            getProduct(\"100\");\n          }).not.toThrow();\n\n    })\n    it(\"should buy product\", () =\u003e {\n        log(\"buyProduct\")\n\n        expect(() =\u003e {\n            buyProduct(\"100\")\n        }).not.toThrow();\n        const listProduct = getProducts();\n\n        expect(listProduct[0].sold).toBe(\n            1,\n            \"sold should increase to 1\"\n        );\n    });\n});\n```\n\nLet check what we got here: ```describe``` invoke the test , and check our test case with the correct order or not. We use ```beforeEach``` to fund near to account test ```alice.near``` and ```setProduct``` BBQ in each successive test.\nNow to run test open your terminal and type\n```\nyarn asp\n```\nAfter the test is finished, you will see that the results of the passing tests will look like this:\n\u003cimg width=\"607\" alt=\"Screen Shot 2022-05-05 at 15 44 55\" src=\"https://user-images.githubusercontent.com/13186215/166892352-fdd0edc6-1a23-4c8d-9963-8fc14c8b59bf.png\"\u003e\n\nYou can refer [my previous project unit test](https://github.com/anhfactor/danger-is-near/tree/master/assembly/__tests__), i've applied in this course.\n\n### TLDR\n```\ngit clone https://github.com/anhfactor/near-101-dacade-unit-test\ncd near-101-dacade-unit-test\nyarn\nyarn asp\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanhfactor%2Fnear-101-dacade-unit-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanhfactor%2Fnear-101-dacade-unit-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanhfactor%2Fnear-101-dacade-unit-test/lists"}