Как я могу смоделировать тег ActsAsTaggableOn :: используя RSpec и Mocha?Я новичок в тестировании рельсов, поэтому я могу упустить что-то очевидное ...
Я попробовал:
/ spec / controllers / books_controller.rb
it "should return 2 categories whith books" do
tag_ruby = mock_model(ActsAsTaggableOn::Tag, :name => 'ruby', :context => 'category')
tag_java = mock_model(ActsAsTaggableOn::Tag, :name => 'java', :context => 'category')
ActsAsTaggableOn::Tag.stubs(:with_context).returns([tag_ruby, tag_java])
get 'books_by_categories'
assigns(:books_by_categories).should be_empty
end
/app / controllers / books_controller
def books_by_categories
@books_by_categories = ActsAsTaggableOn::Tag.with_context('category').all.map { |category|
{:name => category.name, :books => category.books.visibles.limit(10)}
}
end
Я получаю:
Failure/Error: tag_ruby = mock_model(ActsAsTaggableOn::Tag, :name => 'ruby', :context => 'category')
Mocha::ExpectationError:
unexpected invocation: #<Mock:ActsAsTaggableOn::Tag_1001>.__mock_proxy()
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.id(any_parameters)
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.name(any_parameters)
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.marked_for_destruction?(any_parameters)
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.valid?(any_parameters)
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.persisted?(any_parameters)
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.context(any_parameters)
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.destroyed?(any_parameters)
- expected exactly once, not yet invoked: #<Mock:ActsAsTaggableOn::Tag_1001>.blank?(any_parameters)
Спасибо за вашу помощь, Винсент