require_relative File.expand_path '../../test_helper',__FILE__
FactoryGirl.define do
factory :task do
name "testinga again"
finished 0
end
end
class TaskTest < Test::Unit::TestCase
include FactoryGirl::Syntax::Methods
test "should not save tasks without title" do
task = Task.new
assert_equal false, task.save
end
test "should save tasks" do
task = FactoryGirl.create(:task)
assert_equal attributes_for(:task), task
end
end
I want to unit test the task creation process. In the should save task
task, I have saved the value into the db, now I want to test if the saved value is equal to what i really sent to the db. How to do that or Am i even doing it right?
First of all, you are trying to check stuffs that are already been tested by industry leading engineers. If you see the ActiveRecord
project, every API has been tested rigorously. So I recommend you skip this.
But, if your purpose is to learn UnitTesting, then again you are doing it wrong. Unittesting is a black-box testing. You are not supposed to call/invoke any other classes or third party softwares/services like Database
in your case.
Lets call this a Integration Test.
I want to test if the saved value is equal to what i really sent to the db
Its easy; see:
test "should save tasks" do
task = FactoryGirl.build(:task, name: 'John')
task.save
assert_equal 'John', task.reload.name
end
reload
fetches the record from the database.