ruby-on-railsrspectddminitest

Rails tests are passing but server refuses to start on production due to syntax error


This is one weird issue I am facing. All the test cases are passing on the CI/CD but there's a syntax error in one of the files.

And when this changes goes to production the server refuses to start.

Is this the correct expected behaviour?

What are my options to do some kind of sanity check for syntax-related mistakes so that CI/CD fails and doesn't let people go ahead with deployment?


Solution

  • You most likely are not loading the offending class when you're running tests (meaning - you're most probably not testing code that requires loading of that class).

    The different behavior is most likely due to different config between production and test env, namely this:

    config.cache_classes = true
    # or, depending on your rails version (see: https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#eager-loading) 
    config.eager_load = true
    

    Production usually preloads all the classes before starting a server, in development and test environments "lazy loading" is the name of the game (meaning classes are loaded when they are needed).

    Since your CI/CD is running all specs anyway, consider such config in your config/enviroments/test.rb:

    config.cache_classes = ENV.fetch("CI", false)
    # or
    config.eager_load = ENV.fetch("CI", false)
    

    (most CI services are setting CI=true env variable, if you don't have it - you need to implement a way to "tell" your code that it's running on CI.