I would like to know how I could make a request on my development environment from the tests environment.
I know it's not a good practice but I don't know how to achieve my goal otherwise:
On my project, I've created some modules (Newsletter, Search, GuestBook, ...) and I would like to have a table allowing me to enabled or disabled modules. I have created the scaffold for this.
The but of all of this is to not run tests if the setting is disabled in other environments.
My problem is when I make a request to check if the module is enabled or not, it targets the test database and not the development database.
Anyone know how I could solve my problem ?
Thanks for your help
You don't want to do it that way, you want to set the same DB in your test environment so that it will check that table in your test DB during the testing.
...or even better, stub out the model used to retrieve those enabled/disabled settings so you can just adjust them on the fly as you run various tests.
Related
Is it possible to tell rspec to restart Rails before an example is run? I'm building an Engine that hooks into the Rails initialization process and the users can make some configuration changes, in an initializer, that impact how Rails and the Engine are configured. I want to be able to simulate those configuration changes, restart rails and test the result.
I haven't done this feat yet, but as best practice I think your engine tests should be part of the engine and should have minimal dependencies.
Some approaches I've seen and believe you should try and combine:
Mock a minimal parent rails app to test your engine.
Write multiple dummy apps to test with.
Instead of loading the entire rails application, you can split spec_helper and rails_helper in smaller parts, also gaining in setup time.
You can write custom rake tasks to switch environment before spawning a new test thread.
You can also overwrite at runtime the configuration values which reflect in your test (plus: use dependency injection!).
If your initializer is complex enough, you could extract it in a testable helper and wire it up in your test initializers.
Also, there seems to be a gem for that: Combustion.
Can someone explain to me what the Rails environments are and what they do? I have tried researching myself, but could not find anything. From what I gather, the environments are:
Development
Productions
Test
Each "environment" is really just a config. You can launch your app in various different modes, and the modes are called "environments" because they affect the app's behaviour in lots of different ways. Ultimately, though, they are just configs.
BTW you can't have looked very hard when you looked "everywhere", because i just googled "rails environment" and the top result was this
http://guides.rubyonrails.org/configuring.html
which is the official explanation of configuring the rails environment.
From what you have provided in your question, it seems that you are asking:
"What are the difference between each environment configuration in Rails?"
Rails comes packages with 3 types of environments. Each have its own server, database, and configuration. See Rails Guides: Configuration for more information on options available to you.
Setting up the environment
To set your Rails environment, you will want to enter in command line:
export RAILS_ENV=<env>
Where <env> can be test, development, or production. Setting this environment variable is crucial, as it will determine what gems are installed, or what env is touched when running rails console or rails server.
Included in configuration is the gemset used for the app. When you run rails new, you will find a Gemfile with groups test, development, and production. These groups correspond to the environment currently set. When the environment is set to one of those, running bundle install installs all gems related to that group (and gems not listed in a group).
Included environments
test is designed for running tests/specs. This database will likely be bare bones, except for seeds you may call before running the suite. After each test is complete, the database will rollback to its state before the test began. I do not recommend launching rails server, as running tests (via MiniTest or RSpec) will do this for you, and close the server once the suite is finished.
development allows you to "test" your app with a larger database, typically a clone of production. This allows you to test actual real-world data without breaking production (the version that customers or end-users will experience). To view the development environment in action, change the RAILS_ENV and launch rails server. This is good for deciding how you want your pages to look (CSS, HTML). It is also good practice to briefly "test" your app yourself, clicking around making sure everything "looks" good and the JavaScript works.
production is reserved for the customer and end-user. Configuration includes the actual domain of the app, which ports to use, and initializers or tasks to run. You do not want to play around with your database, as it may be customer-impacting. Ideally, the app should work as best as it can, since this is considered your "final product."
Here are some good reads about Rails Environments
http://teotti.com/use-of-rails-environments/
and
https://signalvnoise.com/posts/3535-beyond-the-default-rails-environments
good luck !!
I have set up a bunch of initialising tasks that call various APIs and save them to constants for access in my controller (this may well be bad form, but I don't need them saved to the database for now).
Is there any way to run
rails s
without initializers, such as a setting up a specific environment that ignores them, or would this be bad practice?
There are option to group initializers and then run only certains of them (see this answer), but if this is a temporary situation, you have a lot of options:
Just comment out the ones you don't need (if this is just a test on your machine)
Add a if Rails.env.development? clause to those you don't want to run locally (if this is for all development environments)
I had a problem in a UAT environment because there was a configuration problem in config/environments/uat.rb
Is there a way to test/validate environment files like uat.rb or production.rb to catch errors before deploying?
Loading the files with require in rspec might be a problem because it might affect other tests, not sure about this.
Thanks,
Before deploying on production, I always run my code in production mode on my development machine using environment flag:
rails s --environment=production
You have to choose what tests you write based on the projected ROI (return on investment) of writing those tests. Tests aren't free - far from it. They take time to create and maintain, they junk up your project, etc. Only write tests that you judge to be useful in the future.
I've had plenty of issues with configuration in the past and the solution has always been KISS - Keep it simple stupid. Most apps should require very little configuration, thus are hard to mis-configure.
Is it possible to create user defined Rails environment.... By default support development,staging,production,test... Other than that is it possible for user defined environment.... If yes how? ... please suggest me on this ... thanks in advance...
You can define whatever environments you like by adding a my_awesome_environment.rb file to config/environments. After that, if you want to run a rake task in that environment you could do rake awesome:task RAILS_ENV=my_awesome_environment.
And, unless it's changed recently, there is no staging environment by default. Only development, test, production. As an example of "custom" environments, Cucumber, has its own cucumber environment when you set it up for a project.
If you have a more specific question, we might be able to give a more specific answer. As it stands, this is pretty open ended.