Testing with rspec

Does anyone have examples of rspec integration with Jets? I’m trying to build some rspec tests that need rails_helper.rb and hitting multiple issues.

Here is the error I’m getting now, after hacking around in the various init files:

11:22:52@ maestro (master)*$ bundle exec rspec spec/lib/rules_engine/rules_engine_spec.rb

An error occurred while loading ./spec/lib/rules_engine/rules_engine_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)

NoMethodError:
  undefined method `enable=' for nil:NilClass
# ./config/application.rb:5:in `block in <top (required)>'
# ./config/application.rb:1:in `<top (required)>'
# ./config/environment.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'
# ./spec/lib/rules_engine/rules_engine_spec.rb:2:in `require'
# ./spec/lib/rules_engine/rules_engine_spec.rb:2:in `<top (required)>'
No examples found.

Finished in 0.00022 seconds (files took 1.08 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

Here’s my hacked environment.rb:

require "jets"
require File.expand_path('../application', __FILE__)
Jets.boot

And the line in application.rb it is complaining about: (config.prewarm.enable)

Jets.application.configure do
    config.project_name = "maestro" 
    config.mode = "html"
    config.prewarm.enable = true

Any help appreciated.

Wondering if you have a code sample on GitHub to help. Thanks!

Just made one : https://github.com/thushw/maestro
Thanks!

There were a few issues. Looks like the original jets spec_helper.rb was copied over and replaced with one from a Rails project. Fixes in here: https://github.com/thushw/maestro/pull/1

To get rspec to pass, the fixes were:

  • Remove rails rspec helpers. Jets and Rails code differ quite a bit so using the rails helpers probably won’t work in most cases.
  • Use the spec/spec_helper.rb that is originally generated with jets new
  • Use more generic factory_bot instead

Here’s the output:

rspec output
$ bundle exec rspec
{:message=>"Evaluating lead with ID  against Maestro Campaign 1 : Health"}
{:message=>"Evaluating lead with ID  against Maestro Buyer 2 : Avenge"}
{:message=>"Evaluating lead with ID  against rules for Maestro Buyer 2 : Avenge"}
rules: #<ActiveRecord::Relation [#<MaestroBuyerRule id: 2, maestro_buyer_id: 2, left_operand: "weight", operator: "<", right_operand: "140", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">]>
{:message=>"Evaluating lead with ID  for buyer 2 resulted in false"}
{:message=>"Evaluating lead with ID  against Maestro Buyer 1 : Marchex"}
{:message=>"Evaluating lead with ID  against rules for Maestro Buyer 1 : Marchex"}
rules: #<ActiveRecord::Relation [#<MaestroBuyerRule id: 1, maestro_buyer_id: 1, left_operand: "age", operator: "<", right_operand: "70", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">]>
{:message=>"Evaluating lead with ID  for buyer 1 resulted in true"}
{:message=>"Evaluating lead with ID  against Maestro Buyer 3 : Pinnacle"}
{:message=>"Evaluating lead with ID  against rules for Maestro Buyer 3 : Pinnacle"}
rules: #<ActiveRecord::Relation [#<MaestroBuyerRule id: 3, maestro_buyer_id: 3, left_operand: "weight", operator: "<", right_operand: "160", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">, #<MaestroBuyerRule id: 4, maestro_buyer_id: 3, left_operand: "age", operator: "<", right_operand: "70", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">]>
{:message=>"Evaluating lead with ID  for buyer 3 resulted in true"}
.{:message=>"Evaluating lead with ID  against Maestro Campaign 2 : Health"}
{:message=>"Evaluating lead with ID  against Maestro Buyer 5 : Avenge"}
{:message=>"Evaluating lead with ID  against rules for Maestro Buyer 5 : Avenge"}
rules: #<ActiveRecord::Relation [#<MaestroBuyerRule id: 6, maestro_buyer_id: 5, left_operand: "weight", operator: "<", right_operand: "140", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">]>
{:message=>"Evaluating lead with ID  for buyer 5 resulted in false"}
{:message=>"Evaluating lead with ID  against Maestro Buyer 4 : Marchex"}
{:message=>"Evaluating lead with ID  against rules for Maestro Buyer 4 : Marchex"}
rules: #<ActiveRecord::Relation [#<MaestroBuyerRule id: 5, maestro_buyer_id: 4, left_operand: "age", operator: "<", right_operand: "70", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">]>
{:message=>"Evaluating lead with ID  for buyer 4 resulted in true"}
{:message=>"Evaluating lead with ID  against Maestro Buyer 6 : Pinnacle"}
{:message=>"Evaluating lead with ID  against rules for Maestro Buyer 6 : Pinnacle"}
rules: #<ActiveRecord::Relation [#<MaestroBuyerRule id: 7, maestro_buyer_id: 6, left_operand: "weight", operator: "<", right_operand: "160", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">, #<MaestroBuyerRule id: 8, maestro_buyer_id: 6, left_operand: "age", operator: "<", right_operand: "70", created_at: "2019-05-20 09:21:42", updated_at: "2019-05-20 09:21:42">]>
{:message=>"Evaluating lead with ID  for buyer 6 resulted in true"}
..........................

Finished in 0.28359 seconds (files took 3.04 seconds to load)
27 examples, 0 failures

$

Also, recommend you include on the specific aws-sdk gems that’s needed instead of the whole thing. IE: aws-sdk-s3, aws-sdk-ec2, etc.

1 Like

Thank you very much!