0

I have an initializer to load some data from a csv file to a global variable to be used in a model. Right now it looks like this:

...
XYZ = Hash[*CSV.open...]

Now I want to write an rspec spec for this initializer. I am using rubocop-rspec and it says that it is that describe should take not a string, but a class or a module (https://www.rubydoc.info/gems/rubocop-rspec/1.1.0/RuboCop/Cop/RSpec/DescribeClass).

Assuming this is reasonable, what are the options to convert initializer to a class?

There is an option to use describe XYZ but it does not seem right.

1 Answer 1

1

1) Well, you could wrap Hash[*CSV.open...] into some helper class or module, say, DataLoader, cover it with tests and then invoke like XYZ = DataLoader.do_stuff(filename) in your initializer.

But there is one "issue" with this approach: the class will be really "dumb" (adding no custom logic at all, just wrapping a couple of methods from stdlib) and will be used in an initializer only - so will be called just once. I doubt all this additional boilerplate worth it.

So, I'd probably consider something simpler:

2) Just ignore (disable) this particular cop for this particular case. Conventions are good and rubocop is just great, but sometimes breaking the rules leads to a cleaner code than blindly following them. For example, I sometimes add tests for destructive rake tasks - in this case, I have to break this convention too, and I do it when necessary because it is still better than artificial workarounds for the sake of conventions... Need to test a minor piece of initializer's logic? Just do it :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.