Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(testing): mocking PrevRealm() in tests #3506

Open
matijamarjanovic opened this issue Jan 14, 2025 · 2 comments
Open

bug(testing): mocking PrevRealm() in tests #3506

matijamarjanovic opened this issue Jan 14, 2025 · 2 comments
Labels
🐞 bug Something isn't working

Comments

@matijamarjanovic
Copy link
Contributor

PrevRealm() Returns Empty Path After TestSetRealm

In effort of making unit tests for a token and NFT registry I have accidently come across a minor bug in the testing functions from std. When trying to set the realm using std.TestSetRealm function to the realm that is being tested, the realm returned from calling std.PrevRealm() contains only the address and not the path. The print of that realm looks like this:

code:

	std.TestSetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/tokenhub"))
	println(std.PrevRealm())

print output:
(struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm)

Exactly the same thing gets printed when there is no std.TestSetRealm at all. Is this expected behavior?

Other potential problems

TestSetOrigPkgAddr Has No Effect on PrevRealm

While trying to set the PrevRealm() I tried using the std.TestSetOrigPkgAddr. I got the similar result, as if nothing was set (see code and print output below). Is this supposed to happen since to my understanding TestSetOrigPkgAddr should set the entry realm?

code:

	std.TestSetOrigPkgAddr(std.DerivePkgAddr("gno.land/r/matijamarjanovic/home"))
	println(std.PrevRealm())

print output:
(struct{("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" std.Address),("" string)} std.Realm)

TestSetRealm in Setup Function Not Persisting

Since all my tests are using the same std.TestSetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home")) I have tried making a separate function called setupTests() which would contain the two test set functions (no need for both this was an effort to make it work) but this has had the same result as the previous 2 points - PrevRealm() returning a Realm structure with path == "". Like the function was not even called in the tests.

To conclude, the only way I could set the tests to work is if I gave TestSetRealm an address argument of a realm that is not one being currently tested. Please correct me if I'm wrong and this is expected behavior.

@thehowl
Copy link
Member

thehowl commented Jan 14, 2025

SetRealm sets the value of CurrentRealm, not PrevRealm. This is useful because it is the value of PrevRealm for any other realm that is called afterwards. does that answer your question?

@matijamarjanovic
Copy link
Contributor Author

SetRealm sets the value of CurrentRealm, not PrevRealm. This is useful because it is the value of PrevRealm for any other realm that is called afterwards. does that answer your question?

Well, yes, but I guess what I was trying to say is - without using TestSetRealm you can't access PrevRealm()'s package path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
Status: Triage
Development

No branches or pull requests

2 participants