# Struct with Custom Constructor throwing error during Testing?

**URL:** <https://discourse.julialang.org/t/struct-with-custom-constructor-throwing-error-during-testing/47896>\
**Category:** New to Julia\
**Tags:** package, testing\
**Created:** [October 6, 2020, 9:51pm UTC](https://discourse.julialang.org/t/struct-with-custom-constructor-throwing-error-during-testing/47896 "2020-10-06T21:51:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DataPsycho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datapsycho/32/16891_2.png) [@DataPsycho](https://discourse.julialang.org/u/DataPsycho)\
**Post date:** [October 6, 2020, 9:51pm UTC](https://discourse.julialang.org/t/struct-with-custom-constructor-throwing-error-during-testing/47896/1 "2020-10-06T21:51:54Z")

</div>

Hi,  
I have defined a constructor as follows:

```julia
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:

```julia
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

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

```

The test runs fine.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 6, 2020, 9:54pm UTC](https://discourse.julialang.org/t/struct-with-custom-constructor-throwing-error-during-testing/47896/2 "2020-10-06T21:54:35Z")

</div>

```julia
  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`.

---

<div class="post-metadata">

**Author:** ![DataPsycho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datapsycho/32/16891_2.png) [@DataPsycho](https://discourse.julialang.org/u/DataPsycho)\
**Post date:** [October 6, 2020, 10:01pm UTC](https://discourse.julialang.org/t/struct-with-custom-constructor-throwing-error-during-testing/47896/3 "2020-10-06T22:01:10Z")

</div>

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

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

```

The test file runs fine.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 6, 2020, 10:04pm UTC](https://discourse.julialang.org/t/struct-with-custom-constructor-throwing-error-during-testing/47896/4 "2020-10-06T22:04:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DataPsycho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datapsycho/32/16891_2.png) [@DataPsycho](https://discourse.julialang.org/u/DataPsycho)\
**Post date:** [October 6, 2020, 10:07pm UTC](https://discourse.julialang.org/t/struct-with-custom-constructor-throwing-error-during-testing/47896/5 "2020-10-06T22:07:24Z")

</div>

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