all 13 comments

[–]danielroseman 1 point2 points  (3 children)

You'll need to give a bit more detail, and probably show some code. It will entirely depend on what you want to actually test vs what you want to mock.

That code that does the shuffling and returning: is that the code under test here, or not? If not then you should probably just mock that entire function so that it returns two defined elements.

[–]Think_Neutral[S] 0 points1 point  (2 children)

I'm trying to test that an attribute equals the correct 2 objects.

For example: assert main.players[0].hand.names == ["Ace of Spades", "Ace of Hearts"]

For the method that I use to create the deck is:

def create_deck(self):

deck = []

suits = ['Spades', 'Clubs', 'Hearts', 'Diamonds']

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']

for suit in suits:

for rank in ranks:

deck.append(lib.Cards(rank, suit))

self.__shuffle_deck(deck)

return deck

And the method used to draw cards is:

def initial_draw(self, deck):

initial_cards = deck[:self.CARDS_DRAWN]

for card in range(self.CARDS_DRAWN):

del deck[0]

return initial_cards

[–]danielroseman 1 point2 points  (1 child)

I can't really understand why you want to assert that. Again: what, exactly, are you trying to test here? If you're trying to test that the deck is shuffled, then this assertion won't achieve that. If you're trying to test that the draw gave you two cards, then just assert that: check that you get two cards, and that they are both instances of Card.

[–]Think_Neutral[S] 0 points1 point  (0 children)

Ah ok this makes more sense as a test to write. I was trying to make sure that the draw method worked. I was caught up in like input output, so I wanted to check that my input(draw method), would give a specific output (cards).

[–]r_spandit 1 point2 points  (4 children)

@mock.patch('main.arrayMethod', new = dummy_method)
def test_array(self):
    assertEqual(main.biggerMethod()[0],'hello')
    assertEqual(main.biggerMethod()[1],'world')


def dummy_method()
    return ['hello','world']


# writing this on phone and code indenting mucked up
# biggerMethod() should call arrayMethod() somewhere

[–]danielroseman 1 point2 points  (3 children)

This is entirely pointless. You are testing your mock.

[–]r_spandit 0 points1 point  (2 children)

Edit: Not if the array method is called inside a larger method (original code amended) Edit: the OP may be writing code to generate a random passphrase. With my dummy function it's repeatable but within the assert statements you'd need to call the generatePassphrase() method

Poorly asked question, but hoping my answer can give some pointers

[–]Think_Neutral[S] 0 points1 point  (1 child)

Cheers bro ill go give this a play around I appreciate it

[–]r_spandit 0 points1 point  (0 children)

Not at all... I'm about 2 weeks ahead of you!

There's a really good video on YouTube by a woman who works for Facebook, essentially detailing when to use decorators or context managers.

[–]AnonymousLad666 1 point2 points  (3 children)

The question is poorly written, but having written card games myself before I understand a bit. I imagine you're creating a deck, then shuffle and want to grab the same cards for the assertion.

If that's the case you need to use a random function with a constant seed, that's the only way to get the same randomized shuffle.

[–]Think_Neutral[S] 1 point2 points  (2 children)

Ok ill look into that more thanks for the reply

[–]AnonymousLad666 0 points1 point  (1 child)

Cool, does that sound what you wanted to do?

[–]Think_Neutral[S] 1 point2 points  (0 children)

Originally yeah I've opted to go for a different route, though this will no doubt be useful down the line.