Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tests
- import scala.collection.mutable.Map
- import org.junit.runner.RunWith
- import org.scalatest.junit.JUnitRunner
- import org.scalatest.FlatSpec
- import org.scalatest.Matchers
- import org.scalamock.scalatest.MockFactory
- import auction._
- import auction.event._
- import auction.bots.BidBot
- import auction.event.MessagingEvent
- /**
- * Tests for BidBot
- */
- @RunWith(classOf[JUnitRunner])
- class BotTest extends FlatSpec with Matchers with MockFactory {
- // Tämä luokka on avuksi harjoituksessa ja sitä ei tarvitse muuttaa:
- /**
- * A mock version of Auction to record outgoing bids.
- */
- class MockAuction extends Auction with Dispatcher {
- val makeBidRecorder = mockFunction[User, AuctionItem, Int, Boolean]('makeBid)
- override def makeBid(who: User, item: AuctionItem, amount: Int) = {
- // We use mockedMakeBid to record calls, but eventually call
- // the actual method. This kind of a method double is a _spy_.
- makeBidRecorder(who, item, amount)
- super.makeBid(who, item, amount)
- }
- }
- trait MockedDispatcher extends Dispatcher {
- val mf = mockFunction[AuctionEvent, Unit]('fireEvent)
- override def fireEvent(e: AuctionEvent) = {
- mf(e)
- super.fireEvent(e)
- }
- }
- "BidBot" should "make a bid if the owner has money and someone else makes a new best bid" in {
- val auction = new MockAuction()
- // Vinkki: Käytä testeissä auction.makeBidRecorder funktiota asettamalla sille
- // AuctionTest luokan tapaan odotuksia (expects) ennen kuin aloitat huutamisen.
- val james = new User("James", 123)
- val pete = new User("Pete", 456)
- val jane = new User("Jane", 789)
- val bot = new BidBot(pete, auction)
- //create the item
- val sofa = auction.addAuction(james, "blue sofa", 100, 5)
- //make bot follow the item
- bot.bidOnItem(sofa, 1000, 100)
- //add credits
- pete.buyCredits(1)
- // Tell the mock what to expect.
- inSequence {
- auction.makeBidRecorder.expects(jane, sofa, 300)
- auction.makeBidRecorder.expects(pete, sofa, 400)
- auction.makeBidRecorder.expects(jane, sofa, 500)
- }
- // Bid enough that it succeeds.
- auction.makeBid(jane, sofa, 300)
- auction.makeBid(jane, sofa, 500)
- }
- "BidBot" should "not bid if the owner has the best bid" in {
- val auction = new MockAuction()
- val james = new User("James", 123)
- val pete = new User("Pete", 456)
- val jane = new User("Jane", 789)
- val bot = new BidBot(pete, auction)
- val sofa = auction.addAuction(james, "blue sofa", 100, 5)
- bot.bidOnItem(sofa, 1000, 100)
- //add credits
- pete.buyCredits(10)
- // Tell the mock what to expect.
- inSequence {
- auction.makeBidRecorder.expects(jane, sofa, 300)
- auction.makeBidRecorder.expects(pete, sofa, 400)
- auction.makeBidRecorder.expects(pete, sofa, 500)
- }
- // Bid enough that it succeeds.
- auction.makeBid(jane, sofa, 300)
- auction.makeBid(pete, sofa, 500)
- }
- "BidBot" should "not bid over the limit" in {
- val auction = new MockAuction()
- val james = new User("James", 123)
- val pete = new User("Pete", 456)
- val jane = new User("Jane", 789)
- val bot = new BidBot(pete, auction)
- val sofa = auction.addAuction(james, "blue sofa", 100, 5)
- bot.bidOnItem(sofa, 1000, 100)
- //add credits
- pete.buyCredits(1)
- // Tell the mock what to expect.
- inSequence {
- auction.makeBidRecorder.expects(jane, sofa, 1100)
- }
- // Bid enough that it succeeds.
- auction.makeBid(jane, sofa, 1100)
- }
- "BidBot" should "not bid if the owner has no money" in {
- val auction = new MockAuction()
- val james = new User("James", 123)
- val pete = new User("Pete", 456)
- val jane = new User("Jane", 789)
- val bot = new BidBot(pete, auction)
- //create the item
- val sofa = auction.addAuction(james, "blue sofa", 100, 5)
- //make bot follow the item
- bot.bidOnItem(sofa, 1000, 100)
- // Tell the mock what to expect.
- inSequence {
- auction.makeBidRecorder.expects(jane, sofa, 300)
- }
- // Bid enough that it succeeds.
- auction.makeBid(jane, sofa, 300)
- }
- "BidBot" should "ask for more money after using the last credit" in {
- val auction = new Auction with MockedDispatcher
- // Vinkki: Voit joko kirjoittaa oman kuuntelijan dispatcherille tai korvata
- // dispatcherin kuten luokassa AuctionTest.
- auction.mf expects (where { x: AuctionEvent => x.isInstanceOf[NewAuction] })
- val james = new User("James", 123)
- val pete = new User("Pete", 456)
- val jane = new User("Jane", 789)
- val bot = new BidBot(pete, auction)
- //create the item
- val sofa = auction.addAuction(james, "blue sofa", 100, 5)
- //make bot follow the item
- bot.bidOnItem(sofa, 1000, 100)
- //add credits
- pete.buyCredits(1)
- // Tell the mock what to expect.
- auction.mf expects (where { x: AuctionEvent => x.isInstanceOf[BidRaised] }) repeat (1 to 10)
- auction.mf expects (where { x: AuctionEvent => x.isInstanceOf[BidBeaten] })repeat (1 to 10)
- auction.mf expects (where { x: AuctionEvent => x match {
- case m:MessagingEvent => m.receiver == pete
- case _ => false
- }})
- // Bid enough that it succeeds.
- auction.makeBid(jane, sofa, 300)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement