在我的测试中,我想为任何类的实例存根固定响应。
它可能看起来像:
Book.stubs(:title).any_instance().returns("War and Peace")
然后,每当我打电话给@book.title
,它就会返回“战争与和平”。
有没有办法在MiniTest中执行此操作?如果是的话,你能给我一个示例代码片段吗?
或者我需要像摩卡这样的东西?
MiniTest确实支持Mocks,但Mocks对于我需要的东西来说太过分了。
# Create a mock object
book = MiniTest::Mock.new
# Set the mock to expect :title, return "War and Piece"
# (note that unless we call book.verify, minitest will
# not check that :title was called)
book.expect :title, "War and Piece"
# Stub Book.new to return the mock object
# (only within the scope of the block)
Book.stub :new, book do
wp = Book.new # returns the mock object
wp.title # => "War and Piece"
end
如果你对没有模拟库的简单存根感兴趣,那么在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
我使用minitest进行所有Gems测试,但是使用mocha完成我的所有存根,可能可以使用Mocks完成所有操作(没有存根或其他任何东西,但是模拟非常强大),但我发现mocha做了一个干得好,如果它有帮助:
require 'mocha'
Books.any_instance.stubs(:title).returns("War and Peace")
您可以轻松地在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!
编辑:还有另一种方法,即使它似乎有点hackish,它仍然是你的问题的解决方案:
klass = Class.new Book do
define_method(:title) { "War and Peace" }
end
klass.new.title.must_equal "War and Peace"
你不能用Minitest做到这一点。但是,您可以存根任何特定实例:
book = Book.new
book.stub(:title, 'War and Peace') do
assert_equal 'War and Peace', book.title
end
为了进一步说明@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
。 (注意:使用这种方法时,qzxswpoi方法必须在存根之前存在。)现在,您可以更简单地说:
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
因此,对于任何特定实例,调用stubbed方法的次数超过指定次数会引发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
,但前提是此时在该实例上调用MockExpectationError: expected title()
。
您始终可以在测试代码中创建模块,并使用包含或扩展到猴子补丁类或对象。例如(在book_test.rb中)
#verify
现在您可以在测试中使用它
module BookStub
def title
"War and Peace"
end
end
这允许您将比简单存根可能允许的更复杂的行为组合在一起
我想我会分享一个我在这里建立答案的例子。
我需要在一长串方法的末尾存根一个方法。这一切都始于PayPal API包装器的新实例。我需要存根的调用基本上是:
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
除非方法是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
来使这个工作不只是一个方法。