Danh mục

Growing Object-Oriented Software, Guided by Tests- P5

Số trang: 50      Loại file: pdf      Dung lượng: 0.00 B      Lượt xem: 14      Lượt tải: 0    
Jamona

Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Growing Object-Oriented Software, Guided by Tests- P5: Test-Driven Development (TDD) hiện nay là một kỹ thuật được thành lập để cung cấp các phần mềm tốt hơn nhanh hơn. TDD là dựa trên một ý tưởng đơn giản: các bài kiểm tra Viết cho code của bạn trước khi bạn viết đoạn code riêng của mình. Tuy nhiên, điều này "đơn giản" ý tưởng có kỹ năng và bản án để làm tốt. Bây giờ có một tài liệu hướng dẫn thiết thực để TDD mà sẽ đưa bạn vượt ra ngoài những khái niệm cơ bản. Vẽ trên một...
Nội dung trích xuất từ tài liệu:
Growing Object-Oriented Software, Guided by Tests- P5176 Chapter 16 Sniping for Multiple Items public void hasShownSniperIsBidding(FakeAuctionServer auction, int lastPrice, int lastBid) { driver.showsSniperStatus(auction.getItemId(), lastPrice, lastBid, textFor(SniperState.BIDDING)); } The rest is similar, which means we can write a new test: public class AuctionSniperEndToEndTest { private final FakeAuctionServer auction = new FakeAuctionServer(item-54321); private final FakeAuctionServer auction2 = new FakeAuctionServer(item-65432); @Test public void sniperBidsForMultipleItems() throws Exception { auction.startSellingItem(); auction2.startSellingItem(); application.startBiddingIn(auction, auction2); auction.hasReceivedJoinRequestFrom(ApplicationRunner.SNIPER_XMPP_ID); auction2.hasReceivedJoinRequestFrom(ApplicationRunner.SNIPER_XMPP_ID); auction.reportPrice(1000, 98, other bidder); auction.hasReceivedBid(1098, ApplicationRunner.SNIPER_XMPP_ID); auction2.reportPrice(500, 21, other bidder); auction2.hasReceivedBid(521, ApplicationRunner.SNIPER_XMPP_ID); auction.reportPrice(1098, 97, ApplicationRunner.SNIPER_XMPP_ID); auction2.reportPrice(521, 22, ApplicationRunner.SNIPER_XMPP_ID); application.hasShownSniperIsWinning(auction, 1098); application.hasShownSniperIsWinning(auction2, 521); auction.announceClosed(); auction2.announceClosed(); application.showsSniperHasWonAuction(auction, 1098); application.showsSniperHasWonAuction(auction2, 521); } } Following the protocol convention, we also remember to add a new user, auction-item-65432, to the chat server to represent the new auction. Avoiding False Positives We group the showsSniper methods together instead of pairing them with their associated auction triggers. This is to catch a problem that we found in an earlier version where each checking method would pick up the most recent change—the one we’d just triggered in the previous call. Grouping the checking methods together gives us confidence that they’re both valid at the same time. Testing for Multiple Items 177The ApplicationRunnerThe one significant change we have to make in the ApplicationRunner is to thestartBiddingIn() method. Now it needs to accept a variable number of auctionspassed through to the Sniper’s command line. The conversion is a bit messy sincewe have to unpack the item identifiers and append them to the end of the othercommand-line arguments—this is the best we can do with Java arrays:public class ApplicationRunner { […]s public void startBiddingIn(final FakeAuctionServer... auctions) { Thread thread = new Thread(Test Application) { @Override public void run() { try { Main.main(arguments(auctions)); } catch (Throwable e) { […] for (FakeAuctionServer auction : auctions) { driver.showsSniperStatus(auction.getItemId(), 0, 0, textFor(JOINING)); } } protected static String[] arguments(FakeAuctionServer... auctions) { String[] arguments = new String[auctions.length + 3]; arguments[0] = XMPP_HOSTNAME; arguments[1] = SNIPER_ID; arguments[2] = SNIPER_PASSWORD; for (int i = 0; i < auctions.length; i++) { arguments[i + 3] = auctions[i].getItemId(); } return arguments; }} We run the test and watch it fail.java.lang.AssertionError:Expected: is not null got: null at auctionsniper.SingleMessageListener.receivesAMessage()A Diversion, Fixing the Failure MessageWe first saw this cryptic failure message in Chapter 11. It wasn’t so bad thenbecause it could only occur in one place and there wasn’t much code to testanyway. Now it’s more annoying because we have to find this method:public void receivesAMessage(Matcher178 Chapter 16 Sniping for Multiple Items and figure out what we’re missing. We’d like to combine these two assertions and provide a more meaningful failure. We could write a custom matcher for the message body but, given that the structure of Message is not going to change soon, we can use a PropertyMatcher, like this: public void receivesAMessage(Matcher Testing for Multiple Items 179 To add multiple items, we need to distinguish between the code that establishesa connection to the auction server and the code that joins an auction. We startby holding on to connect ...

Tài liệu được xem nhiều: