Solidity TypeError – Resolving Contract testAdoption Not Convertible to Address

solidity

pragma solidity ^0.5.0;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Adoption.sol";

contract testAdoption{
    Adoption adoption = Adoption(DeployedAddresses.Adoption());

    //Testing the adopt() function
    function testUserCanAdoptPet() public{
        uint returnedId = adoption.adopt(8);

        uint expected = 8;

        Assert.equal(returnedId, expected, "Adoption of pet ID 8 should be recorded");
    }

    //Testing retrieval of a single pet's owner
    function testGetAdopterAddressBypetId() public {
        //Expected owner is this contract
        address expected = this;

        address adopter = adoption.adopters(8);

        Assert.equal(adopter, expected, "Owner of pet ID 8 should be recorded");
    }

    //Testing retrieval of all pet owners
    function testGetAdopterAddressBypetIdInArray() public {
        //Expected owner is this contract
        address expected = this;

        //Store adopters in memory rather than contract's storage
        address[16] memory adopters = adoption.getAdopters();

        Assert.equal(adopters[8], expected, "Owner of pet ID 8 should be recorded");
    }


}

TypeError: Type contract testAdoption is not implicitly convertible to
expected type address.
address expected = this;
^———————^

Best Answer

The variable expected is of type address, while this is of type contract testAdoption. You can cast a contract to an address to make it work.

address expected = address(this);