come testato con PHPUnit:assertEquals e assertTrue danno risultati diversi per le variabili stesse
$xml_1 = new SimpleXMLElement('<name>Bugs</name>');
$xml_2 = new SimpleXMLElement('<name>Bugs</name>');
$this->assertEquals($xml_1, $xml_2); // Passes
$this->assertTrue($xml_1==$xml_2); // Fails
Um, che cosa?
MODIFICA: No, questa non è una domanda stupida. In Python:
import unittest
class TestEqualityIdentity(unittest.TestCase):
def test_equality(self):
x = 1
y = 1
self.assertTrue(x==y) # Passes
self.assertEqual(x, y) # Passes
if __name__ == '__main__':
unittest.main()
Nessuna ragione per cui PHP deve comportarsi come Python. Ma non è una domanda stupida neanche in PHP.
$x = 1;
$y = 1;
$this->assertEquals($x, $y); // Passes
$this->assertTrue($x==$y); // Passes
EDIT 2 risposta di Raymond sotto è giusto, non importa che in questo scritto è di 3 voti verso il basso.
FWIW, Avevo bisogno di un test di confronto tra i valori del nodo di testo di due oggetti XML e l'ho ottenuto lanciandoli su stringhe.
$this->assertTrue((string) $xml_1== (string) $xml_2); // Passes, works in if test
// Note that simply referring to a SimpleXMLElement _seems_ to give its
// text node.
$this->assertEquals($xml_1, 'Bugs'); // Passes
// This seemed weird to me when I first saw it, and I can't
// say I like it any better now
Direi che == verifica l'uguaglianza tra puntatori e assertEquals cerca l'uguaglianza di valore. Ma non ne so abbastanza per saperlo con certezza. Google dovrebbe essere in grado di aiutarti qui –
Aurelio, grazie per l'utile modifica sul titolo. – chernevik
@chernevik Prego. Inoltre ... +1 - questa non è una domanda stupida. –