I have a concept problem, I am working on a rails project & as per my knowledge both the test & spec are used for tests. Where test refers to unit testing & spec refers to integration testing. But I want to have to have full idea about those two folders & if they both refers to test then what is the major difference between them?
Rails default testing framework gives you test folder for you rails default testing framework, if you want to use RSpec testing framework, which gives you folder called 'spec' RSPEC, it is your wish to select which option, RSPEC specs are more readable, people use both ways.
Related
Is there a difference between a feature spec and system spec? Both seem to use Capybara to test the full application yet they have different docs on RSpec.
System specs are built on top of Rails own system tests while feature specs were implemented by RSpec before Rails had system tests. If you're starting a new project write system specs, but if you have existing feature specs there's really nothing to be gained currently by changing them to system specs since functionality wise they're basically identical.
In my Rails 4 app, I'm using Rspec for testing. My directory structure is
spec
-- controllers
-- factories
-- features
-- spec_helper.rb
-- support
When I run rspec spec, it runs my tests in controllers, but not in features. I can specify rspec spec/features and they'll run, but I want to be able to run all tests under spec at once. My guess is it's not looking in features because of a configuration setting, but I don't know where this is.
I've tried different incantations of starting rspec but haven't had any luck.
Based on your feedback to the comments above, the issue is one of file naming. I've definitely been burned by that before too. By default Rspec will go through the files looking for ones ending with _spec.rb, this default behaviour is overridden if you specify the folder manually.
I have a good understanding of the differences between unit and intergration tests in RoR in theory. I'm using rspec and capybara to do a lot of testing on my site. What I don't understand is how do you run different tests? If I do
bundle exec rspec
it will run all of my specs in all of my spec folders (model, controller, views, integration, etc). If I want to run an integration test, is it as simple as ?
bundle exec rspec spec/integration
I know there are some differences between these types of test behind the scenes. Specifically this problem (which I also have) has me thinking about unit vs. integration: How to test for a redirect with Rspec and Capybara
What does Rails do differently when running integration tests? The solution posted in the above problem is
Checking for redirect is not supported in rspec-rails request specs,
but is supported in Rails integration tests.
So how do I make one of my tests an integration test? Is it just a matter of putting it in the right spec folder?
As I understand it rspec is designed specifically for unit testing purposes, thus you'll need to use something else for integration testing. The message seems it imply that Rails can do integration tests as well. I don't have any experience or knowledge of testing in rails alone, but I do know of the cucumber gem that is built very well for integration tests.
It has it's own domain specific language and other quirks you'll need to get used to but it should have the capability you're looking for.
I develop Rails 3 app with Rspec. For automatically testing i use autotest gem which is great but... But project grows, i have more and more files and autotest has strange rules - sometimes it tests files which i previously changed, sometimes it tests whole project (and it takes a LOT of time). Do you know any useful gem which allows to test automatically in background + it lets you choose what exactly to test, for example, models specs, or only controllers, or integrations tests, or whole project?
Guard will watch your spec files and project files and run the relevant specs (IE, run the model spec after changing that model), and has a good amount of customization
https://github.com/guard/guard-rspec
let's say you added a controller and action (example: story/index), and want to run a functional test by
rake test:functionals
and then you found that another part of the project your coworker is working on actually broke the test at an earlier place (another controller/action), before your functional test takes place.
In this case, can you run just one functional test, which is yours?
Wise-Ass Answer
If you coworker breaks the tests, then he should fix the test, or he shouldn't have committed the code to the repo. That is the main principle that we usually have in projects I work on.
Nice Answer
Try this
rake test:functionals TEST=test/functional/xy_test.rb
Or this
running one test without rake but with explicit $-loadpath works too
here "ruby -I directory" specifies the $loadpath. Otherwise you won't load test environment and "require test_helper" fails!
ruby -I test test/functional/xy_test.rb
1) Perhaps: rake test:units
2) This link might also help you out:
http://rake.rubyforge.org/
3) This might also help you out:
"Typical Rails tests come in the
follow forms:
Unit (Model) These test business logic
in your models. A well-written Rails
application should have the bulk of
its code in its models, so the bulk of
your tests should be these.
Functional (Controller) These test
individual controller actions in
isolation.
Integration (Controller to Controller)
These test state mutations
between/over multiple actions and
routing, i.e. ensuring that things
don’t totally explode as a user clicks
through a typical work flow.
Fixtures Used to hold example model
data used to easily instantiate those
models in tests, avoiding the tedious
process of manually creating model
objects.
Unit/Helpers These test helpers used
in views.
In other words, the basic
relationships look like this:
Model Unit Test Controller Functional
Test View (as part of a) Functional
Test Controller to
Controller Integration Test"
Found at http://rails-nutshell.labs.oreilly.com/ch09.html
Check out the single_test gem which makes it easy to run specific tests (or groups of tests)