vendredi 6 février 2015

checking valid? for model using minitest for Rails 4.1.7

I am new to rails. I was trying to write unit test(mintest 5.5.1) for a model. the Database i am using is a SQL server (i use the gem activerecord-sqlserver-adapter (4.1.0)) . The tables are created fine.


Migration Script for model



class CreateRpReports < ActiveRecord::Migration
def change
create_table :rp_reports do |t|
t.string :name, :null => false
t.string :description

t.timestamps
end
end
end


Model Class



class RpReport < ActiveRecord::Base
validates :name, presence: true

def errors
{:name => ["can't be blank"]}
end
end


Unit test for RpReport Model



require 'test_helper'

describe RpReport do

def valid_params
{ name: "John Doe", description: "John Report" }
end

####TEST 1###
it "is valid with valid params" do
rpReport = RpReport.new valid_params
rpReport.must_be :valid? #must create with valid params
end

####TEST 2###
it "is invalid without a name" do
params = valid_params.clone
params.delete :name
rpReport = RpReport.new params

rpReport.wont_be :valid? #must not be valid without name

rpReport.errors[:name].must_be :present? #must have error for
missing name
end
end


I am getting error when i run rake test


Test Output



RpReport
test_0001_is valid with valid params FAIL (0.01s)
Minitest::Assertion: Expected #<RpReport id: nil, name: "John Doe", description: "John Report", created_at: nil, updated_at: nil> to be valid?.
test/models/rp_report_test.rb:21:in `block (2 levels) in <top (required)>'

test_0002_is invalid without a name ERROR (0.00s)
NoMethodError: NoMethodError: undefined method `add' for {:name=>["can't be blank"]}:Hash
test/models/rp_report_test.rb:31:in `block (2 levels) in <top (required)>'
test/models/rp_report_test.rb:31:in `block (2 levels) in <top (required)>'


Finished in 0.04500s
2 tests, 1 assertions, 1 failures, 1 errors, 0 skips


Can anyone assist in pointing out what mistake i am doing while writing the test


Aucun commentaire:

Enregistrer un commentaire