# Test our marketplace · Decentralized marketplace with FT4 · Learn Chromia

> Write a test to confirm the marketplace behaves correctly. You'll assemble it piece by piece with the Rell test framework. Open…

Source: https://chromia.com/learn/marketplace-course/module-assets/test-marketplace/

Decentralized marketplace with FT4Lesson 14 of 15

1. [01Decentralized marketplace with FT4](https://chromia.com/learn/marketplace-course/introduction/)
2. [02Set up your project](https://chromia.com/learn/marketplace-course/setup/)

Module 1: Registering accounts and assets

1. [03Overview](https://chromia.com/learn/marketplace-course/module-ft4/)
2. [04Register payment token](https://chromia.com/learn/marketplace-course/module-ft4/register-token/)
3. [05Create user accounts](https://chromia.com/learn/marketplace-course/module-ft4/register-account/)

Module 2: Model NFTs in Rell

1. [06Overview](https://chromia.com/learn/marketplace-course/module-nft/)
2. [07Define the NFT model](https://chromia.com/learn/marketplace-course/module-nft/nft/)
3. [08Mint NFTs](https://chromia.com/learn/marketplace-course/module-nft/mint-nfts/)
4. [09Make card attributes random](https://chromia.com/learn/marketplace-course/module-nft/randomness/)

Module 3: Create the marketplace

1. [10Overview](https://chromia.com/learn/marketplace-course/module-assets/)
2. [11Charge a fee for mystery card purchases](https://chromia.com/learn/marketplace-course/module-assets/buy-mystery-card/)
3. [12Put a card up for sale on the marketplace](https://chromia.com/learn/marketplace-course/module-assets/list-card/)
4. [13Buy a card on the marketplace](https://chromia.com/learn/marketplace-course/module-assets/buy-listed-card/)
5. [14Test our marketplace](https://chromia.com/learn/marketplace-course/module-assets/test-marketplace/)
6. [15Test using Chromia CLI](https://chromia.com/learn/marketplace-course/module-assets/test-cli/)

[Learn](https://chromia.com/learn/)[Decentralized marketplace with FT4](https://chromia.com/learn/marketplace-course/)

14 / 15Module 3: Create the marketplaceAdvanced

# Test our marketplace

Write a test to confirm the marketplace behaves correctly. You'll assemble it piece by piece with the Rell test framework. Open `src/rell_marketplace_test/blockchain_rell_marketplace_test.rell` and add these imports:

src/rell\_marketplace\_test/blockchain\_rell\_marketplace\_test.rell

```
@test module;

import rell_marketplace.*;
import lib.ft4.test.core.{ ft_auth_operation_for };
import lib.ft4.assets.{ get_asset_balance };
import lib.ft4.external.accounts.{ get_accounts_by_signer };
```

The test also needs a query that returns the last account and your NFTs. You'll reuse this query in a later lesson when testing with the Chromia CLI.

src/rell\_marketplace/queries.rell

```
query get_last_account() = account @ {} ( $, @omit @sort_desc .rowid ) limit 1;

query get_all_nfts() {
    return nft_card @* {} (
        nft_card_dto(
            strength = .strength,
            health = .health,
            owner_id = .nft.owner.id,
            nft_id = .nft.rowid
        )
    );
}

struct nft_card_dto {
    strength: integer;
    health: integer;
    owner_id: byte_array;
    nft_id: rowid;
}
```

Next, define a test function called `test_list_and_buy_nft`. Inside it, create two test accounts along with the `economy_account`.

src/rell\_marketplace\_test/blockchain\_rell\_marketplace\_test.rell

```
function test_list_and_buy_nft() {
    val alice = rell.test.keypairs.alice;
    val trudy = rell.test.keypairs.trudy;
    val economy_account = rell.test.keypairs.frank;

    rell.test.tx()
        .op(create_user(alice.pub)).sign(alice)
        .run();

    val alice_account = get_last_account();

    rell.test.tx()
        .op(create_user(trudy.pub)).sign(trudy)
        .run();

    val trudy_account = get_last_account();
}
```

Once Alice owns a mystery card, add the step that puts it up for sale. First, retrieve the card minted by the earlier operation; it is the first and only one. Then call the `list_nft` operation with Alice as the signer.

src/rell\_marketplace\_test/blockchain\_rell\_marketplace\_test.rell

```
    rell.test.tx()
        .op(ft_auth_operation_for(alice.pub))
        .op(buy_mystery_card()).sign(alice)
        .run();

    val nft = get_all_nfts()[0];
    rell.test.tx()
        .op(ft_auth_operation_for(alice.pub))
        .op(list_nft(nft.nft_id.to_integer(), 20))
        .sign(alice)
        .run();
```

With the card listed, Trudy can now purchase it from the marketplace at the listed price.

src/rell\_marketplace\_test/blockchain\_rell\_marketplace\_test.rell

```
    rell.test.tx()
        .op(ft_auth_operation_for(trudy.pub))
        .op(buy_nft(nft.nft_id.to_integer()))
        .sign(trudy)
        .run();
```

Finally, check the outcome: compare the account balances and confirm who now owns the minted NFT.

src/rell\_marketplace\_test/blockchain\_rell\_marketplace\_test.rell

```
    val nft_after_trade = get_all_nfts()[0];

    assert_equals(get_asset_balance(alice_account, dapp_meta.asset), 920);
    assert_equals(get_asset_balance(trudy_account, dapp_meta.asset), 980);
    assert_equals(nft_after_trade.owner_id, trudy_account.id);
```

The test is now complete. Run it from your terminal with the `chr test` command. A passing run confirms that the marketplace works as intended.

The next lesson walks through a comparable test using the Chromia CLI.

Written for this site from Chromia’s public course. Code samples are the course’s own.

[← Previous**Buy a card on the marketplace**](https://chromia.com/learn/marketplace-course/module-assets/buy-listed-card/)[Next →**Test using Chromia CLI**](https://chromia.com/learn/marketplace-course/module-assets/test-cli/)
