Currently I'm writing a unit test a method which the only thing that I need from Rails is to use the url helper methods, like 'product_url'
I really don't want to load the whole Rails environment, as we all know it's a bit expensive, just to be able to run the url helper methods.
Is that a way of loading only part of Rails environment, just the necessary to run these methods?
Thanks!
Alex
As far, as I know it is not so easy. But you can use spork server to have rails testing environment in memory to make your tests run fast and smooth.
I'm with #Nick_Kugaevsky– in fact, I'm a lot more negative than him. I just don't think it's possible. To have the concept of a helper, Rails has to load ActionPack, which is not very useful by itself. I guess you could figure out a way to work directly with ActionPack, but I'd be extremely surprised if that were possible.
You can try to mock all *_path in your tests.
Related
While rspec automatically creates specs for any helpers created by the Rails generators, I was wondering if other Rails developers find it important/useful to spec the helpers in real-world or if they often don't bother, since the helpers are often tested by proxy through testing of the components that use them?
Personally I do test helper methods, because I like to test them in isolation. If the following feature specs fails I know I probably made a mistake in my test setup because I already ensured that the helper method works.
It is also easier to test all possible scenarios. If you want to test all possibilities as part of a whole you need more test setup and sacrifice performance.
Ideally, you want to write tests for everything, but in real world with time constraints, it is not uncommon to skip simple helper method tests because you implicitly test them while building the actual test. In the same way some developers may skip private method tests.
Let's say you're working on a rails app that has a lot of initializers that call out to various external systems at startup.
When running rspec with rspec-rails it runs all the initializers, even if your test is something simple that doesn't require rails infrastructure.
I know you can use Spork to only incur this cost once but is there a way to not incur it at all? It seems silly to load up all of rails just for a simple PORO spec.
Don't use spork. If you want to use a preloader, look into zeus or spring.
You don't need to load your entire rails environment to test things that don't depend on rails. This can be as simple as explicitly requiring the dependencies you need per spec, or creating an entirely separate minimal_spec_helper for non-rails things or a rails_spec_helper for rails things.
Do your initializers really "call out to various external systems"? If this is what it sounds like, that your initializers are making external network/HTTP calls, that sounds like a terrible idea.
We have a small method that some of our other teams use internally. I'm writing tests for it, but I have run in to a small issue:
The method itself checks to ensure the request comes from a specific server (request.host). I have tried stubbing, but I perhaps was stubbing the wrong controllers? I tried the controller I was testing and .any_instance, then I tried controller.any_instance, but neither worked.
I have a hunch that I might be able to spoof it using devise, but so far google has yet to yield much usefulness.
I feel mildly stupid for not trying this first, but:
In a test where I am trying to spoof request.host, the way to set this in your corresponding test is:
drumroll please...
request.host = dev.example.com
If you are testing subdomains, I have a writeup with some code here: http://www.chrisaitchison.com/2013/03/17/testing-subdomains-in-rails/
The name pretty much says it all. Does anyone know how to accomplish functional testing when you are not using migrations in Rails? I'd be open to any advice or third party libraries (if there are any). I thought of creating my own plugin to address this but it seems like a pretty big task and would rather not do this unless necessary.
Thanks in advance.
http://github.com/jpignata/temping
Temping allows you to create arbitrary ActiveRecord models backed by a temporary SQL table for use in tests.
It appears that there is no "easy" way to do this. I'm looking into RSpec rails to see if it can be modified to not use migrations.
I'm developing a Rails app, and I was just talking with my colleague that we have a mix of fixtures and mocks in our tests, which we're doing using cucumber and Rspec. The question would be: when should each one be used?
I would use a mock object when using the real object is impracticable/not necessary. Lets say for example you need to call some remote API such as an address finder via zip code. You would probably want to mock the object so the calls on it aren't actually made each time you run your tests. There are other reasons too such as improving speed, asking for data that changes where you need an exact response or perhaps it doesn't exist yet. It allows you to test things in isolation as you can determine that when you call these methods on this mock object you will get this back and you don't actually need to run the code as for this test it's not important.
If you use fixtures you will have a real object and the methods etc will be called and their code run, unless of course you stub the methods out, which is something for another question.
Hope that helps a little. There is a good peepcode (http://peepcode.com/products/rspec-mocks-and-models) on mocking and stubbing, maybe check it out.