How to simulate the entrypoint caller in testing

How do you simulate the entrypoint caller address when testing? Getting the error “Sender is undefined” error on an assert statement, which I halfway expected

File “”, line 12, in
assert sp.sender == sp.address(“tz1da6oerX2dpWHBEvnBUkcuCKQRK9b9EgTo”), “Only contract owner can call this entrypoint”
Sender is undefined: sp.sender

The function code

@sp.entrypoint
def add_eligible_delegator(self, address):
assert sp.sender == sp.address(“tz1da6oerX2dpWHBEvnBUkcuCKQRK9b9EgTo”), “Only contract owner can call this entrypoint”
#tz1da6oerX2dpWHBEvnBUkcuCKQRK9b9EgTo
this_address = sp.cast(address,sp.address)
self.data.approved_delegators[this_address] = sp.record(yay=False,nay=False,_pass=False,votingPower=0)

The test scenario call

c1.add_eligible_delegator(valid_delegator_1.address)

Looked like in previous versions of smartpy you could add the run() method to the testing call, with the sender or source parameters, but I’m assuming run() is no longer available.

Having already learned quite a bit with this small exercise, I’m willing to add this question: does anyone already have voting contract code I can steal? I want to give my delegators a chance to voice their preference for the latest proposal

In a test, to set the address that is calling the entrypoint, use _sender, as in this example:

import smartpy as sp

# Contract
@sp.module
def main():
    class MyContract(sp.Contract):
        def __init__(self, admin):
            self.data.admin = admin
            self.data.counter = 0

        @sp.entrypoint
        def myEntryPoint(self):
            assert self.data.admin == sp.sender
            self.data.counter += 1


# Tests
@sp.add_test()
def test():
    scenario = sp.test_scenario("test_scenario", main)

    admin = sp.test_account("admin").address
    c1 = main.MyContract(admin)
    scenario += c1
    scenario.verify(c1.data.counter == 0)

    # Call the entrypoint as admin
    c1.myEntryPoint(_sender = admin)
    scenario.verify(c1.data.counter == 1)

    # Verify that you can't call as any other address
    alice = sp.test_account("alice").address
    c1.myEntryPoint(_sender = alice, _valid = False)
    scenario.verify(c1.data.counter == 1)

For examples of voting contracts, go to the SmartPy IDE and open the Multisig contracts templates – maybe there will be something you can use in there.

Appreciate the quick response. That worked