在我的测试中,我想为任何类的实例存根固定的响应。
它可能类似于:
Book.stubs(:title).any_instance().returns("War and Peace")
然后每当我打电话给@book.title
时,它就会返回“战争与和平”。
有没有办法在MiniTest中执行此操作? 如果是的话,你能给我一个示例代码片段吗?
或者我需要像摩卡这样的东西吗?
MiniTest确实支持Mocks,但Mocks对于我需要的东西来说太过分了。
答案 0 :(得分:31)
book = MiniTest::Mock.new
book.expect :title, "War and Piece"
Book.stub :new, book do
wp = Book.new
wp.title # => "War and Piece"
end
答案 1 :(得分:27)
如果你对没有模拟库的简单存根感兴趣,那么在Ruby中这很容易做到:
class Book
def avg_word_count_per_page
arr = word_counts_per_page
sum = arr.inject(0) { |s,n| s += n }
len = arr.size
sum.to_f / len
end
def word_counts_per_page
# ... perhaps this is super time-consuming ...
end
end
describe Book do
describe '#avg_word_count_per_page' do
it "returns the right thing" do
book = Book.new
# a stub is just a redefinition of the method, nothing more
def book.word_counts_per_page; [1, 3, 5, 4, 8]; end
book.avg_word_count_per_page.must_equal 4.2
end
end
end
如果你想要更复杂的东西,比如对一个类的所有实例进行存根,那么它也很容易做到,你只需要有点创意:
class Book
def self.find_all_short_and_unread
repo = BookRepository.new
repo.find_all_short_and_unread
end
end
describe Book do
describe '.find_all_short_unread' do
before do
# exploit Ruby's constant lookup mechanism
# when BookRepository is referenced in Book.find_all_short_and_unread
# then this class will be used instead of the real BookRepository
Book.send(:const_set, BookRepository, fake_book_repository_class)
end
after do
# clean up after ourselves so future tests will not be affected
Book.send(:remove_const, :BookRepository)
end
let(:fake_book_repository_class) do
Class.new(BookRepository)
end
it "returns the right thing" do
# Stub #initialize instead of .new so we have access to the
# BookRepository instance
fake_book_repository_class.send(:define_method, :initialize) do
super
def self.find_all_short_and_unread; [:book1, :book2]; end
end
Book.find_all_short_and_unread.must_equal [:book1, :book2]
end
end
end
答案 2 :(得分:21)
我使用minitest进行所有Gems测试,但是使用mocha完成所有存根,可能可以使用Mocks完成所有操作(没有存根或其他任何东西,但模拟非常强大),但我发现摩卡做得很好,如果它有帮助:
require 'mocha'
Books.any_instance.stubs(:title).returns("War and Peace")
答案 3 :(得分:18)
您可以轻松地在MiniTest
中存根方法。这些信息可在github获得。
因此,按照您的示例,并使用Minitest::Spec
样式,这就是存根方法的方法:
# - RSpec -
Book.stubs(:title).any_instance.returns("War and Peace")
# - MiniTest - #
Book.stub :title, "War and Peace" do
book = Book.new
book.title.must_equal "War and Peace"
end
这是一个非常愚蠢的例子,但至少可以为你提供如何做你想做的事情的线索。我尝试使用 MiniTest v2.5.1 这是 Ruby 1.9 附带的捆绑版本,看起来在这个版本中,#stub方法尚不支持,但是我尝试使用 MiniTest v3.0 ,它就像一个魅力。
祝你好运并祝贺使用 MiniTest !
编辑:此外还有另一种解决方法,即使看起来有些过时,它仍然可以解决您的问题:
klass = Class.new Book do
define_method(:title) { "War and Peace" }
end
klass.new.title.must_equal "War and Peace"
答案 4 :(得分:13)
你不能用Minitest做到这一点。但是,您可以存根任何特定实例:
book = Book.new
book.stub(:title, 'War and Peace') do
assert_equal 'War and Peace', book.title
end
答案 5 :(得分:11)
为了进一步说明@panic's answer,我们假设您有一个Book类:
require 'minitest/mock'
class Book; end
首先,创建一个Book实例存根,并使其返回所需的标题(任意次):
book_instance_stub = Minitest::Mock.new
def book_instance_stub.title
desired_title = 'War and Peace'
return_value = desired_title
return_value
end
然后,让Book类实例化您的Book实例存根(仅在以下代码块中):
method_to_redefine = :new
return_value = book_instance_stub
Book.stub method_to_redefine, return_value do
...
在此代码块(仅限)中,Book::new
方法是存根的。我们来试试吧:
...
some_book = Book.new
another_book = Book.new
puts some_book.title #=> "War and Peace"
end
或者,最简洁:
require 'minitest/mock'
class Book; end
instance = Minitest::Mock.new
def instance.title() 'War and Peace' end
Book.stub :new, instance do
book = Book.new
another_book = Book.new
puts book.title #=> "War and Peace"
end
或者,您可以安装Minitest扩展程序gem minitest-stub_any_instance
。 (注意:使用此方法时,{stub}之前必须存在Book#title
方法。)现在,您可以更简单地说:
require 'minitest/stub_any_instance'
class Book; def title() end end
desired_title = 'War and Peace'
Book.stub_any_instance :title, desired_title do
book = Book.new
another_book = Book.new
puts book.title #=> "War and Peace"
end
如果您要验证Book#title
被调用了一定次数,请执行以下操作:
require 'minitest/mock'
class Book; end
book_instance_stub = Minitest::Mock.new
method = :title
desired_title = 'War and Peace'
return_value = desired_title
number_of_title_invocations = 2
number_of_title_invocations.times do
book_instance_stub.expect method, return_value
end
method_to_redefine = :new
return_value = book_instance_stub
Book.stub method_to_redefine, return_value do
some_book = Book.new
puts some_book.title #=> "War and Peace"
# And again:
puts some_book.title #=> "War and Peace"
end
book_instance_stub.verify
因此,对于任何特定实例,调用stubbed方法的次数多于指定的次数会引发MockExpectationError: No more expects available
。
此外,对于任何特定实例,调用stubbed方法的次数少于指定的次数会引发MockExpectationError: expected title()
,但前提是此时在该实例上调用#verify
。
答案 6 :(得分:5)
您始终可以在测试代码中创建一个模块,并使用include或extend来扩展猴子补丁类或对象。例如(在book_test.rb中)
module BookStub
def title
"War and Peace"
end
end
现在您可以在测试中使用它
describe 'Book' do
#change title for all books
before do
Book.include BookStub
end
end
#or use it in an individual instance
it 'must be War and Peace' do
b=Book.new
b.extend BookStub
b.title.must_equal 'War and Peace'
end
这允许您组合比简单存根可能允许的更复杂的行为
答案 7 :(得分:1)
我想我会分享一个我在这里建立答案的例子。
我需要在一长串方法的末尾存根一个方法。这一切都始于PayPal API包装器的新实例。我需要存根的呼叫基本上是:
paypal_api = PayPal::API.new
response = paypal_api.make_payment
response.entries[0].details.payment.amount
我创建了一个返回自己的类,除非方法是amount
:
paypal_api = Class.new.tap do |c|
def c.method_missing(method, *_)
method == :amount ? 1.25 : self
end
end
然后我将其存入PayPal::API
:
PayPal::API.stub :new, paypal_api do
get '/paypal_payment', amount: 1.25
assert_equal 1.25, payments.last.amount
end
通过制作哈希并返回hash.key?(method) ? hash[method] : self
,您可以使其不仅仅用于一种方法。