开发者

How should I test this, do I mock since it is hitting the db?

I have a method that does this:

Given a array of tags, it tries to fetch the Tag by its name from the db if it exists, otherwise it creates a new Tag. It then returns a collection of all the tag objects.

tag_text = "general tag1 tag2"

the Tag.rb model has a name property that would be in the db if it exists.

How should I go about testing the method:

 def find_tags_from_tag_text (tag_text)

          tags = []

          tag_text.split.each do |x|
    开发者_运维问答           t = Tag.find_by_name(x)

               if t.nil?  
                 t = Tag.new(name => x).save!
               end

               tags << t

          end

    end


Rails testing guide http://guides.rubyonrails.org/testing.html covers pretty much what you are trying to do, have you checked it out? TL;DR: 1) prepare fixtures - data to initialize blank testing database with, 2) write tests to compare actual results with expected results of your model's methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜