开发者

How do I test a model that's not in the models folder with RSpec?

I'm using the acts_as_taggable_on gem and monkeypatching ActsAsTaggableOn::Tag in an intitializer (config/initializers/acts_as_taggable_on.rb). I wrote a spec for my code and put it in spec/models/tag.rb, but when I run rspec spec or autotest, that file is not included in the specs that get run. I also tried putting it in a directory to match the constant hierarchy (spec/models/acts_开发者_运维知识库as_taggable_on/tag.rb), but it still didn't run. How do I get RSpec to recognize this spec file? I'm using Rails 3.0.3 and RSpec 2.4.0.


You should name your spec-file as tag_spec.rb. Rspec expects every spec-file to end in _spec.rb. The folder is only relevant to know what your describe is about (a model, a controller, ...). In your case i think it doesn't matter, and i would place it in the spec/lib instead of the spec/models folder.


Why is this file in config? It belongs in the lib directory because it is code of your own that doesn't "fit" into the app directory. When you require "spec_helper" in your test this loads the Rails environment which should load all the models and therefore all the files that the models require (this is the important part) and so you'll be able to test it that way.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜