onflow/cadence-tools

[Test] Unable to use events as a return type

Opened this issue · 1 comments

Current Behavior

I am not able to use an event signature as a return type in the cadence testing framework:
image

Expected Behavior

It would be great to allow these kinds of return signatures so I can cut down on copy/paste code. Usually I need the same type of event over and over when writing tests and was hoping I could write a helper to achieve this more easily

Steps To Reproduce

import Test
import "Foo"

// some tests here
...

// this method is invalid
pub fun getLatestEvent(): Foo.SomeEvent {
    return Test.eventsOfType(Type<Foo.SomeEvent>()).removeLast() as! Foo.SomeEvent
}

Environment

- Cadence version: v1.9.2
- Network: Testing

Unfortunately this comes from Cadence itself. Events are currently not "first-class" citizens, in that they cannot be used as argument types / return types or be constructed and assigned to variables. However, we can use their type for type-casting, as you have done here:

let events = Test.eventsOfType(Type<Foo.SomeEvent>()).removeLast() as! Foo.SomeEvent

The above methods contain the business logic for which CompositeTypes can be used as argument types & return types.