I'd like to be able to use Rails' test fixture helpers in Cucumber, but the information provided on the Cucumber wiki seems... out of date at best: https://github.com/cucumber/cucumber/wiki/Fixtures (heading: Fixture Helper Methods)
Google has been less than helpful.
Is there a good way to do this?
Update: Tried using ActiveRecord::TestFixtures to no avail it seems like this is the class to do the job, but can't get it mixed into the World correctly
One of the reasons there's so little information on fixtures is that most rails developers are now all using Factory Girl to generate test data.
Factory Girl provides you mush more control over both the data that's generated (and when it's created), but it also allows you a lot more control of the associated model classes you inevitably will need.
After an hour of brain cramming, I was able to make it work.
Replace
fix = Fixtures.cached_fixtures(ActiveRecord::Base.connection, table_name)[fixture_symbol.to_s]
with
fix = Fixtures.cached_fixtures(ActiveRecord::Base.connection, table_name).first.fixtures[fixture_symbol.to_s]
I have already updated the wiki at https://github.com/cucumber/cucumber/wiki/Fixtures
Related
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.
can we load seed data when start cucumber?
Please support me a way.
You could use Factory Girl to in your cucumber tests to setup your 'stuff'
Background:
A car exists
Scenario: I drive a car
Given I am in a car
And I have keys in the ignition
When I turn the keys
...
Then you'll create the car in your step definitions, with something like
#car = Factory.create(:car)
I prefer this approach:
https://github.com/cucumber/cucumber/wiki/fixtures
I'm not opening the fixtures vs factories debate, of course, just saying that I've yet to see a case where files of data (seed, or otherwise) cease to be useful.
Once yaml fixtures are defined, they can be instantiated procedurally via Fixtures.create_fixtures per above, or set up as rake tasks.
They're just plain files, not code intended to have side effects - I have more confidence letting non-technical people add their data to fixtures files (esp. CSV).
I currently have some large strings that I would like use as test data when testing a log scraper. It would work well to have these strings in a YAML file.
I would love to refer to them while testing in a short and concise manner such as:
log_lines(:incorrect_string)
Is there anyway to do this using fixtures, or are they exclusively for use with a corresponding model?
If your yaml looks like this:
:incorrect_string: blah
then you can just
logs = YAML::load_file('filename')
p logs[:incorrect_string]
cheer!
This seems like a simple question but I can't find the answer anywhere. I've noticed that in general, tests in a Ruby on Rails app can be written as:
test "the truth" do
assert true
end
or
def the_truth
assert true
end
It seems newer material writes tests the first way, but I can't seem to find a reason for this. Is one favored over the other? Is one more correct? Thanks.
There has been a shift in recent years from short, abbreviated test names to longer, sentence-like test names. This is partly due to the popularity of RSpec and the concept that tests are specs and should be descriptive.
If you prefer descriptive test names, I highly recommend going with the test method. I find it to be more readable.
test "should not be able to login with invalid password" do
#...
end
def_should_not_be_able_to_login_with_invalid_password
#...
end
Also, because the description is a string it can contain any characters. With def you are limited in which characters you can use.
I believe the first method was implemented starting with Rails 2.2.
As far as I am aware, it simply improves readability of your code (as def can be any function while test is used only in test cases).
Good luck!
As Mike Trpcic suggests you should check out RSpec and Cucumber. I'd like to add that you should also take a look at:
Shoulda (http://github.com/thoughtbot/shoulda/tree/master)
Factory Girl (http://github.com/thoughtbot/factory_girl/tree/master)
Shoulda is a macro framework for writing concise unit tests for your models/controllers, while the second is a replacement for fixtures.
I would suggest doing your testing with either RSpec or Cucumber. I use both to test all my applications. RSpec is used to test the models and controllers, and Cucumber tests the Views (via the included Webrat functionality).
I have model called test, and test can have many tests, and should be able to have a reference to it's parent test if it exists. EG
test <-- parent doesn't exist
test
test
test
test
test
test
test <-- parent doesn't exist
i've seen a couple of possible solutions with examples before 2.3, but how models handle references seems to have changed a lot since 2.0. I was wondering if anyone has a best route option for doing this with some 2.3 spiffiness. Any suggestions?
If each test can have one parent and many children, this seems like an ideal use for acts_as_tree or acts_as_nested_set.