比如说这段代码 isbns = "" books.each do |p| isbns.insert(0,"#{p.isbn}_") end 谢啦。。
Can you specify what do you really want to test here?
If you are testing the logic inside the each
, you can do
result = { ab: "xxx" } # your expected output here
actual = books.each { |p| isbns.insert(0,"#{p.isbn}_") }
expect(result).to eq(actual)
#1 楼 @JIAZHEN 不好意思啊,我不是很懂测试怎么写,只能把代码粘给你看,我希望能按照这样的形式写出来。。但是 cmd 里面告诉我 nomethoderror:each,我真的不是很懂。。。
describe "author search" do before(:each) do @book1 = BookInStock.new("1111", "title1","author1", "genre1", 11.1,11) @book2 = BookInStock.new("2222", "title2","author1", "genre2", 22.2,22) end context "required book in the database"do it "puts book in both remote cache and localcache" do expect(@sqlite_database).to receive(:authorSearch).with('author1').and_return(@book1,@book2) expect(@dalli_client).to receive(:get).with('author1_Statement').and_return(nil) expect(@dalli_client).to receive(:get).with('author1_Statement').and_return(nil) expect(@dalli_client).to receive(:get).with('bks_author1').and_return(nil) #expect(@data_access).to receive(:each).with(@book1).and_return('1111_')
#2 楼 @wxliuzifan 哪一行报错?这里可能性很多光这样贴出代码很难 debug..
另外
expect(@sqlite_database).to receive(:authorSearch).with('author1').and_return(@book1,@book2)
你这里表达的意思是让@sqlite_database调用authorSearch
方法,参数是author1
, 然后第一次会返回@book1, 第二次返回@book2, 如果@sqlite_database根本没有这个方法,那当然会抛出NoMethodError
根据你的表达,我觉得你是想用 mock
allow(@sqlite_database).to receive(:authorSearch).with('author1').and_return(@book1,@book2)
然后当你接下来调用@sqlite_database.authorSearch('author1') 时,第一次会返回@book1, 第二次返回@book2
Make sense? FYI - https://coderwall.com/p/e80caq/rspec-allow-vs-expect
在 books.each
上加一行 binding.pry
, 然后在命令行看看 books 是个啥对象 (nil?), 应该是更前面 books = ...
的代码有问题
#5 楼 @wxliuzifan The RSpec Book
If you're a QA, learn cucumber
and automation.