Storing to database from rspec

Hi,

I want to write a spec to test whether my code is hitting the database or not .

In my model spec file I tried different ways to save the database into my to database such as @user.save! or creating new user by User.create .

Is there any way to store data in database with rspec code .

Thnaks, Nallamani

Nallamani wrote:

I want to write a spec to test whether my code is hitting the database or not .

In my model spec file I tried different ways to save the database into my to database such as @user.save! or creating new user by User.create .

Is there any way to store data in database with rspec code .

Folks do that all the time.

Why don't you think the data is in the database? Can you read the data back, in the spec, and print it out?

Most test rigs run with transactions turned on. Google for [rspec transactions]. They roll back all database changes after each specification.

Yaa I can read the data back . But it is not storing in the database for some reason .

Every time I am creating a new user , the can see an incremental user id . But none of the data is getting stored in database .

Thanks, Nallamani

Philip,

Here is my code in my spec

before (:each) do

@user1 = User.new(:login => "testing", :email => "jijiji@yahoo.com", :password => "hihowru", :password_confirmation => "hihowru")     @user1.save!

end

I can't see this data getting stored in database

Nallamani, when are you checking for the data? Philp's point about transactions is that all the data is rolled back out of the db after the test. If you're expecting to look at the table after the testing is done you won't see anything.

Thanks Andy ,

I wasted two days on Rspec and story runner . Is there any way to delay the roll back .

With normal integration testing I can store in database .

Nallamani wrote:

I wasted two days on Rspec and story runner .

And you will continue to spend quality time with them!

> Is there any way to delay the roll back .

Almost all testing on databases should roll the database back after each test case. The only general exception I can think of is tests that help build a new database engine.

What do you want to do with that data, in the database? If you intend to inspect it, then you should add a .should (or an assertion) to your test case, to inspect that data automatically. Then the transaction will work for you, not against you.

If you are putting the data in the database for some non-test purpose, look at migrations.

> With normal integration testing I can store in database .

Your test_helper.rb may have had use_transactional_fixtures = false.

Nallamani,

Use rake spec:db:fixtures:load to create data for testing the interface in dev mode or use Migrations to seed data for deployment

If your testing model functionality use .should if you are just wanting to test that ActiveRecord works.... DON'T

best of luck

Thanks Philip and Pratt for your timely help . I will stick to integration testing for time being and later shift to rspec .