So I know unit tests go in spec/models, but where do constants defined in initializers and YAML files go?
Ex: This is in my ups_api.yml file:
testing_server:
confirm_url: https://wwwcie.ups.com/ups.app/xml/ShipConfirm
accept_url: https://wwwcie.ups.com/ups.app/xml/ShipAccept
This is loaded in my config/environment.rb. Where/how would I test the value of these constants?
I see no value in testing this sort of thing. You write tests to ensure the correct behavior of code. A configuration setting doesn't involved any kind of logic.
Related
Question
There are many ways to achieve that goal but I would like to know what is the best way to tests the presence of the environmental variables inside a Ruby on Rails project.
Context
We recently had a production issue related to a missing environment variable in one of our Rails project.
To prevent this from happening again, I would like to test the presence of the environments variable in the application.yml configuration file.
I am using Ruby 2.5, Rails 4.2, Spring 2.
Unless checking this specifically in application.yml is an absolute requirement (why?), here is my take. Make a config/initializers/env.rb and put in there something like
%i[FOO BAR BAZ].each do |var|
ENV[var] = ENV.fetch(var)
end
What this does is reads all of your required environment variables (FOO, BAR, BAZ etc.) and Hash#fetch them which will 'raise' if this variable is not set at boot time.
I am trying to get my geb-spock functional tests to run in a specified order because SpecA will create data required for SpecB during its run.
This question is about running the specifications in order, not the individual test methods within the specification.
I have tried changing the specification name to indicate execution order but that didn't work. I found a solution where a Test Suite was used, and the tests were added to the suite in order, but I can't find how to make a test suite work in Grails.
Explicitly specifying them as grails test-app functional: SpecA SpecB , is not a long term option, as more specs will be added.
For sequential or whatever the sequence you want to run your tasks, I do the following thing in my build.gradle file:
def modules = ["X", "Y", "Z", "ZZ"]
if (modules.size() > 1) {
for(j in 1 .. modules.size()-1 ) {
tasks[modules[j]].mustRunAfter modules[values[j-1]]
}
}
Hope that helps. Cheers!
Not really an answer to your question but a general advice - don't do this. Introducing data setup dependencies between test classes will make your suite brittle in the long run. Reasoning about what the state is at a given point will get harder and harder as the amount of tests grows and the global state size with it. Later on hanging a test or introducing a new one might break many tests downstream. This is just asking for trouble.
Ideally, you want to setup the data needed by a test immediately before that test and tear it down afterwards. Grails Remote Control plugin and test data fixture builders are your friends here.
You should define your initialization code in a single place, and if it's shared between both Specs, it may be a good idea to create a superclass with methods you can call in each Spec's set up methods, or a whole class devoted to declare testing methods to reuse.
In any case, the purpose of a unit test is only to test a single functionality, and it shouldn't be responsible of setting up other tests as well.
Question context:
let say that there is some really important row in config/locales/en.yml that is crucial to exist.
en:
foo:
bar: "bubla!"
I don't want to test every single line but
I don't want the test to be too brittle (so no I18n.t('foo.bar').should =~ /bubla/)
so way I'm testing currently is like this
#spec/locals_spec.rb
require 'spec_helper'
describe I18n do
it do
I18n.t('date.datepicker').should be_kind_of(String)
end
end
this way I'm just ensuring that translation exist and that it don't continues (e.g. 'foo.bar.car.lol'
but still I'm not satisfied
Question: What's the best practice to test I18n translations with RSpec and where in spec folder I should place them ?
Check this StackOverflow question for some ideas. My preferred way is this answer on the same question.
Update: These days I tend to use the i18n-tasks gem to handle testing related to i18n, and not what I wrote above or have answered on StackOverflow previously.
I wanted to use i18n in my RSpec tests primarily to make sure that I had translations for everything ie there were no translations missed. i18n-tasks can do that and more through static analysis of my code, so I don't need to run tests for all I18n.available_locales anymore (apart from when testing very locale-specific functionality, like, for example, switching from any locale to any other locale in the system).
Doing this has meant I can confirm that all i18n keys in the system actually have values (and that none are unused or obsolete), while keeping the number of repetitive tests, and consequently the suite running time, down.
i think that i would write an acceptance test for such a "crucial" thing.
in most cases you need the translation in some specific context, ie. displaying something in a datepicker. i would test that context using capybara or whatever works with a javascript driver.
just testing that this translation exists is useless if you don't have the context that it's used within.
Does anyone know how to test page_caching in Rails using RSpec without having to check to see if the cache file has been created for each request? Something more like Controller.performs_page_caching(:action).should be_true?
I've tried looking over the net, but I haven't found anything that works.
I have come up with a solution. You override the caches_page class method for the ApplicationController class and setup an after filter which sets a header 'x-page-cached' to true. Then in your test scripts include a macro for page_cached? which will check to see if response.headers['x-page-cached'] is true or not. Only do this for the test and development environments.
Seems like integration test. For example you could try write request spec and count somehow number of db queries.
I've got a document management system which I programmed in Rails 2.3.8 a while ago and I've been retrofitting some rspec tests to the project before refactoring and making enhancements.
The problem is a lot of my tests require stubbing out most of the File & FileUtils libraries as there is a lot of file interaction within models. Is there a better way to test File & Directory actions without having to touch the filesystem at all?
For instance I stub out mkdir_p:
FileUtils.stub!(:mkdir_p)
And when I'm moving I use something like this:
FileUtils.should_receive(:mv).with("from path","to path")
Use fakefs. It's perfect for the purpose.
I would not recommend to alter File or FileUtils classes, because these changes will affect all your running unit tests. It will be hard to track these changes when you have a lot of different tests and your tests will start affecting each other (which is generally bad testing practice).
I propose to explicitly use a simple manual Dependency Injection approach like constructor injection:
class UnderTest
def initialize file_creator = FileUtils
#file_creator = file_creator
end
def use_file_utils
#file_creator.mkdir('some_dir')
end
end
In RSpec test:
fake_file_utils = mock "FileUtils"
test = UnderTest.new fake_file_utils
test.use_file_utils