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.
Related
I've got some model code that calls to the database with a simple find():
#thing = Thing.find(id)
I have data seeded in the database for the test environment. If I open the console in test (rails c -e test), I can run Thing.find(1) and get a result fine, however when I run a test that calls the method shown above, it reports that it cannot find a record with the id of 1.
I assume I am misunderstanding the relationship between test seed data and the tests being run against that database. Why do I see seed sin the test DB but the test doesn't?
A more conventional way of doing this would be to create fixtures (or factories if using FactoryBot) and call these factories in your test setup. As previous answers have said, hardcoding test ID's will likely return RecordNotFound due to auto-incrementing.
I am new to rails and have been recently going through a range of tutorials and want to make sure that I go down the test driven development route. I have hit a snag on how to approach writing this test.
I have two related models : Task and Project.
Task belongs_to Project and Project has_many Tasks
Inside the project show template I list all the associated tasks to that project.
On the task I have a status field which I want to set to archived on press of a button and then return the back to the associated project.
Would anybody be able to give me a nudge into the right direction for writing tests with associations please. It would be very much appreciated.
What kind of testing setup do you have? Are you using RSpec or Test::Unit? As far as testing the associtation (in Test::Unit) you could do something like that:
#project_test.rb
def setup
#project = Project.new(...)
end
def test_task_association
assert_equal(#project.tasks, [])
task = #project.task.new(...)
assert_equal(#project.tasks, [task])
end
I typed this on the fly so I wouldnt bet the syntax is correct. But the gist of it should be understandable. This sort of test is the "lowest" level test, also called a "unit test" you could create to test
the associations. If you wanted to test the template you could look into "integration testing." These should be pretty helpful: http://guides.rubyonrails.org/testing.html
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
I am trying to test a controller with RSpec but am having a problem because a function in the controller requires a database.
the line of code in the controller looks something like:
#myallresources = Myrsources.all
where Myresources just inherits from ActiveRecord::Base
however, because there is no database, there is nothing to load and #myallresources is just an empty array, causing the test to fail. Is there a way to connect to a database while running the rspec?
I am very new to RSpec and rails so any help would be very appreciated. Thanks.
You shouldn't use a database connection in your controller specs.
Check the section about database isolation on this page http://rspec.info/rails/writing/controllers.html
Basically you have to mock or stub your ActiveRecord models, as those should be tested separately in the models specs. Here's a simple example using mock_model:
before do
mocks = (1..3).map { mock_model(MyResource) }
MyResource.should_receive(:all).and_return(mocks)
end
Put this inside the same block where reside the describe definition testing for the actions that use MyResource.all.
You can find good explanation of mocks and stubs in following links:
http://relishapp.com/rspec/rspec-rails/v/2-5/docs/mocks/mock-model
http://relishapp.com/rspec/rspec-rails/v/2-5/docs/mocks/stub-model
After scaffolding a new class, rails creates the correspondig tests for each controller method.
What do you think, is best practice in a strikt TDD approach? Is it better, to leave these default tests untouched and to create new tests for each new logic? (Even, if they overlap and verify almost the same things?) Or is it ok to extend these default tests with new assertions?
TIA, rufus!
Remove the default tests if they don't test stuff you need tested. If you leave them, you're padding your numbers, but the tests won't actually help you in the long run.
Just like with the scaffolding views and controller, you end up replacing most of the default code with your own, it's just a great place to get started.
In general, I would say remove them if you're not using them, or build on them if they can be extended to fit your needs.