Struct with Custom Constructor throwing error during Testing?

Hi,
I have defined a constructor as follows:

struct ResourceParams
    screen_name::String
    user_id::Union{Integer, Nothing}
    since_id::Union{Integer, Nothing}
    count::Union{Integer, Nothing}
    max_id::Union{Integer, Nothing}
    trim_user::Union{Integer, Nothing}
    exclude_replies::Union{Integer, Nothing}

    # Custom Constructor for the Type Signature
    ResourceParams(
        screen_name;
        user_id = nothing,
        science_id = nothing,
        count=1,
        max_id=nothing,
        trim_user=0,
        exclude_replies=1,
    ) = (
    count isa Integer && count < 1 ?
    error("Count must have to be greter than or equal to 1") :
    new(screen_name, user_id, science_id, count, max_id, trim_user, exclude_replies)
    )
end

Its executing if I run it in the REPL. But when running the test pkg> test it throws error on the error line as follows ERROR: LoadError: LoadError: Count must have to be greter than or equal to 1 Does any one knows the problem?

I have not implemented any Test. The test file is empty as follows:

using MyPackage
using Test

@testset "myfile.jl" begin
    # Write your tests here.
    @testset "Validate Types" begin
    end
end

If I commented out the the line

# count isa Integer && count < 1 ?
# error("Count must have to be greter than or equal to 1") :

The test runs fine.

Thanks in advance!

  count isa Integer && count < 1 ?
    error("Count must have to be greter than or equal to 1") :
    new(screen_name, user_id, science_id, count, max_id, trim_user, exclude_replies)

Either count isn’t an Integer, or it isn’t less than 1. In this case, it’s probably not less than 1 since the kwarg says count=1.

Hi, I have not implemented any test for that Type the test file is empty. But it throws error without any test. If I commented out the the line

# count isa Integer && count < 1 ?
# error("Count must have to be greter than or equal to 1") :

The test file runs fine.

You are initializing that type somewhere. The stacktrace for the error will tell you where if you’ve lost track of it. The error is being thrown from inside the constructor.

1 Like

You are correct. My Stupid mistake :grimacing: I have initialized that inside of another file and forgot to comment it out. :stuck_out_tongue_winking_eye: