Current trend in testing models in a rails3 + datamapper application - ruby-on-rails

What is the current trend for testing models in a Rails3+DataMapper application. I want to use RSpec but sorely miss the concise testing provided by shoulda macros. Question - Is there a way of getting the best of both worlds, ie. a nice dsl for testing and the brevity of shoulda macros, which can be used to test datamapper models.

Check out remarkable. That will do exactly what you want :)
UPDATE [9/8/2012] : Unfortunately while it is still possible to use remarkable in rails 3, it is still the same old 4.0.0.0pre version. So on now rails projects I generally use shoulda-matchers instead. Not as extended, but covers most stuff, and it is maintained :)

Related

Why is RSpec so popular with Rails 5 API guides/tutorials?

I have previously build web-apps with Ruby on Rails 5 and have been using the in built Mini test as the testing suite. On starting API dev on RoR, I see most of the guides and blogs use RSpec for testing out Rails 5 APIs.
My question is is RSpec better for testing APIs than mini-test ? and whether now learning RSpec would be worth it for me ? Would learning RSpec be much harder for me ?
I think the key differentiator is personal preference.
I find RSpec's syntax and support for all the different tests (model, view, controller, feature, helper, services, etc) very clear and easy to implement, also mocking and stubbing is relatively trivial compared to minitest,and also rspec has a large fanbase from the rails community.But when building a gem i advice using minitest since it's the official test suite,but in testing large and complex codes,Rspec makes it easier IMO.

Can Nightwatch be used to test Rails?

A contractor for our startup installed the Selenium-based Nightwatch testing framework, since our stack is React-heavy. But he told me that it could even be used to test our Rails code. A new contractor said, to the contrary, that Nightwatch couldn't do unit tests of our Rails controllers and models (which makes sense to me).
Who is right? Do you suppose the first programmer had in mind just that we would do end-to-end testing (certain inputs lead to certain outputs), and that we need not test the details of the Rails code? Do we, as I suspect and as the new contractor asserts, need RSpec or some other Ruby-based testing framework to handle our Rails code, if we want to be a TDD shop?
Yes it can be used to test Rails. But only from the outside (only through the Browser). So no Unit/Controller/View Tests.
You'll need MiniTest or Rspec for those.
My two cents (also see comment by #SteveCarey): Since I prefer to stick with what comes with Rails and use as little external tools as possible:
Have a look at System Tests that have been introduced with Rails 5.1 or, if you are on a older version, see if you can write those tests using Capybara/Integration Tests.
Update:
You can find more details on testing framework here: http://guides.rubyonrails.org/v4.1/testing.html#brief-note-about-minitest
It was Test::Unit and nowadays is Minitest. But the basics are the same so it does not really matter.
Another popular testing framework is RSpec. Which you can use instead of Minitest/TestUnit if you want to. I prefer Minitest but there are pros and cons for both frameworks.
Rails 4.2 came with Unit-tests, Functional/Controller-tests and Integration-tests. The built in thing that resembles Nightwatch the most are Integration-tests: http://guides.rubyonrails.org/v4.1/testing.html#integration-testing
You can also look at libraries such as Capybara (https://github.com/teamcapybara/capybara) which calls itself an Acceptance Test framework. It integrates nicely with TestUnit/Minitest/Rspec.

Is Feature/Scenerio a replacement for Describe/It?

I've been following the Ruby on Rails Tutorial. The author uses Rspec/Capybara's Describe/It for the whole tutorial. But recently I've seen people using Feature/Scenerio instead. I wonder if Feature/Scenerio is a replacement of Describe/It or something that is used together? If it is a replacement, is there any reason of prefer it over Describe/It?
The same question came up to me lately too. And I went to Capybara's github page to read more about it (search for scenario). Apparently feature, scenario and background are just aliases to make acceptance tests more readable. And I guess that's for legacy reasons -- cucumber uses those keywords. Therefore by aliasing them, capybara acceptance specs read more like traditional cucumber specs.
So no, feature/scenario/background are not a replacement of describe/it/before. As a group of methods, it is only an alternative for acceptance specs. It's up to you and your team to decide which ones to go with.

What problems does Steak gem solve?

I have few integration tests in Capybara+RSpec for a Rails project. Today I encountered Steak gem that meant to be pure Ruby alternative to Cucumber. Though at first glance I don't see any value in either of them. For me it looks like Steak renames describe to feature and it to scenario. Personally i would prefer describe and it because i got used to them and i don't see any reason to make my acceptance tests to look different from my controller or model tests.
Am I missing something?
Steak is RSpec+Capybara. Steak is also these three other things: the name for this Acceptance BDD approach (so that people knows how you test just but using that name), a gem that makes that approach as convenient as possible (so that you don't have to create the same directories and helpers for each project) and a community of developers using it (so that if you have problems, you know where to ask).
For additional rationale, you may want to check out the "Why Steak?" section in the project's README, or this other StackOverflow awswer.

How do you develop outside-in Rails app using Cucumber & RSpec?

I just get started using BDD in Rails application, but I'm not sure what are best practices and workflows? And what other things that I really need for testing for my project such as step definitions, controllers, models, and views? Do I need to test all of those?
I generally think of Cucumber as a way to do integration testing on your application. Combined with Webrat, you can test user workflows, views and so on in a great way. For unit tests, you'll want to go down to a lower level and test your models just with rspec. You may also want to do some functional tests on the controllers, and I probably wouldn't use Cucumber for that either.
Here are a couple of videos:
http://confreaks.com/videos/72-mwrc2009-bdd-with-cucumber
http://rubyconf2008.confreaks.com/rspec-and-cucumber.html
Ryan Bates has some good Railscasts on these topics:
Beginning with Cucumber
Webrat
More on Cucumber
This may be a matter of taste, but having tried out Rspec I prefer using the built-in Rails testing framework along with a gem called Shoulda. In my opinion, that combination lets you write much clearer, more succinct and understandable tests than Rspec by far. But not everyone would agree.
Shoulda's contexts let you organize your tests into logical hierarchies which really helps when you're trying to test all the possible paths some crazy, branching situation, like user logs in with right pw, wrong pw, right pw but registration not confirmed, etc.
In addition be sure to install the ZenTest gem. That lets you just execute the command $ autotest and your tests will run automatically every time you change a file.

Resources