TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to unit test that a radio button is setting state var correctly

Forums > SwiftUI

I have a state var:

@State var gameType: Int = 1

and I have two radio buttons that set it to either 1 or 2:

gameTypeRadioButtonView(index: 1, selectedIndex: $gameType)
gameTypeRadioButtonView(index: 2, selectedIndex: $gameType)

I did a record UI Unit test that launches the app and clicks the button to set gameType from it's initial default of 1 to 2

How do I validate that in the unit test? Like there's plenty of samples for statictext and others, but I can't find anything for just verifying that the radio button did the thing.

related: how would one test that text that should only be visible when gameType is 2 is working? i.e.:

 if gameType != 1 {
                Text("Blue Score")
                Text(String(bluePlayerScore))
                .frame(width: 25, height: 22, alignment: .center)
 }

(yes, I know I can test this other ways, but it's a class project and they want unit tests so...)

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.