添加article model测试及相关fixture数据

This commit is contained in:
刘惊坤 2016-01-09 21:35:43 +08:00
parent 6fa5e4db59
commit 7f4c891f4a
3 changed files with 50 additions and 0 deletions

6
test/fixtures/articles.yml vendored Normal file
View File

@ -0,0 +1,6 @@
article1:
id: 1
title: "aaaaaaaaaaaa"
tags: "aaa"
content: "aaaaaaaaaaaaa"

6
test/fixtures/categorys.yml vendored Normal file
View File

@ -0,0 +1,6 @@
category1:
id: 1
name: "category"
category2:
id: 2
name: "category2"

View File

@ -0,0 +1,38 @@
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
fixtures :articles
setup do
@article = Article.find(1)
@newarticle = Article.new
end
test "test_article_create" do
@newarticle.title = 'bbbbbbbbbbb'
@newarticle.tags = 'bbbb'
@newarticle.content = 'bbbbbbbbbbbbbbbb'
@newarticle.category_id = 1
assert @newarticle.save
end
test "test_article_read" do
assert_instance_of Article, @article
assert_equal 1, @article.id, "test Article.id"
assert_equal "aaaaaaaaaaaa", @article.title, "test Article.title"
assert_equal "aaa", @article.tags, "test Article.tags"
assert_equal "aaaaaaaaaaaaa", @article.content, "test Article.content"
end
test "test_article_update" do
@article.category_id = 2
assert @article.save, @article.errors.full_messages.join("; ")
@article.reload
assert 2, @article.category_id
end
test "test_article_delete" do
@article.destroy
assert_raise(ActiveRecord::RecordNotFound) { Article.find(@article.id) }
end
end