Verify database records in functional testing

Posted by Zhimin Zhan on November 30, 2009|
Comments

Function testing, in a certain way, is verifying the data (retrieved or stored at the database) through the user interface layer. Sometimes, it might be easier to verify database records directly, some SQL skills are required though. RWebSpec provides an easy way to do so, in a easy 3 steps.

  1. Connect to the database
  2. Choose the table
  3. Use easy syntax or SQL to do verification
 story "Check Mysql database" do
    connect_to_database mysql_db(:host => "localhost", :database => "lavabuild_local", :user => "root", :password => ""), true
    load_table("projects") # now can use Project.xxx for easy-to-user datbase query on this table

    Project.count.should == 1  # record count
    Project.first.name.should == "BuildMonitor"
    Project.last.name.should == "BuildMonitor"
    Project.exists?(:name => "BuildMonitor")
  end

Check out the verify_database_spec.rb in demo project.