Using fixtures without a corresponding model in Rails - ruby-on-rails

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!

Related

Test page_caching with RSpec

Does anyone know how to test page_caching in Rails using RSpec without having to check to see if the cache file has been created for each request? Something more like Controller.performs_page_caching(:action).should be_true?
I've tried looking over the net, but I haven't found anything that works.
I have come up with a solution. You override the caches_page class method for the ApplicationController class and setup an after filter which sets a header 'x-page-cached' to true. Then in your test scripts include a macro for page_cached? which will check to see if response.headers['x-page-cached'] is true or not. Only do this for the test and development environments.
Seems like integration test. For example you could try write request spec and count somehow number of db queries.

Using Rails test fixture helpers in Cucumber

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

Rails Fixtures without Name

Is it possible to create fixtures without a name? Something like:
--:
name: ...
description: ...
--:
name: ...
description: ...
No, you can't you'll have to use a random name. If you take a look at the insert_fixtures function you'll notice that it depends on a fixture label to do it's work. To see this in action, try creating a sample yml file like this:
# sample.yml
a:
value: 'hi'
a:
value: 'world'
And then loading it in Ruby:
YAML.load(File.open('sample.yml').read)
You'll get this result:
{"a"=>{"value"=>"world"}}
That's because of the way YAML loads hashes. The keys are distinct, so you need to have unique keys in your yaml files. That's why the fixtures code depends on a unique label.
In the end, your fixtures will be added to your test database anyway, creating models and calling save, so why spending time converting the XML to YAML or CSV when you can just parse your XML and seed your database?
This way you'll have less work to do when adding new data to your tests or extending the scope of your testing.

Rails - how to strip forward slash characters from data in database?

I have an app that I've converted over from another cms. The old URLs were being stored in the database like so:
/this-is-an-old-permalink/
And I need them to be like this:
this-is-an-old-permalink
Note the absence of forward slashes. What is the easiest way to go about removing them?
I'm not necessarily looking for the exact code to do it (although that'd be nice!) -- I'm asking also as a Rails newb: What is the best method to go about doing things like this? I've only really worked with Rails in setting up a model, controller, views and outputting data. I haven't had to do any processing like this. Would it go in the model? Any help is appreciated!
edit
Do I need to get all records, loop through them, do regex on that one field and then save it?
Since you're likely only going to write this once, your best bet is to create a script for it within lib, or to write a migration for it. I recommend the latter, because it will then be executed automatically with rake db:migrate if you restore from your old backup at a later date. You can then use all your standard Model processing tricks (like you would use on a Controller) within the migration without exposing the substitution code to a Controller.
EDIT:
You can add the following to a new file within lib/tasks to create a new rake task for this called db:substitute_slashes:
namespace :db do
desc "Remove slashes from old-style URLs"
task :substitute_slashes => :environment do
Modelname.find(:all).each do |obj|
obj.fieldname.gsub!(/regex here/,'')
obj.save!
end
end
end
The exclamation on the end of save! means it will throw an exception if the resulting object fails validation, which is a good thing to check for in this case.
You would then be able to execute this with the command rake db:substitute_slashes.

Cucumber and Seed data

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).

Resources