background preloader

Unit-Test

Facebook Twitter

Moq

Rhino Mocks vs Moq – Best .Net Mocking Frameworks. I’ve heard multiple people state that they prefer Moq over Rhino Mocks because the Rhino Mocks syntax isn’t as clean.

Rhino Mocks vs Moq – Best .Net Mocking Frameworks

And while I don’t have a strong preference between the two, I’d like to help dispel that myth. It is true that, if you choose the wrong syntax, Rhino can be more complicated. This is because Rhino has been around for a while and it’s original syntax pre-dates the improved Arrange/Act/Assert syntax. I go into a lot more detail on Rhino Mocks and the appropriate way to use it , but to dispel the myth that Rhino Mocks is complicated I would like to compare the syntax for creating stubs and mocks using Rhino vs. Moq: Moq Syntax: //Arrange var mockUserRepository= new Mock<IUserRepository>(); var classUnderTest = new ClassToTest(mockUserRepository.object); mockUserRepository .Setup(x => x.GetUserByName("user-name")) .Returns(new User()); //Act classUnderTest.MethodUnderTest(); //Assert mockUserRepository .Verify(x => x.GetUserByName("user-name"));

Partial Mocks explained. If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.

Partial Mocks explained

Jeff is also one of the minds behind Gallio and MBUnit. You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem. My problem was that his solution was better than mine. Much better. Where it led me was to re-investigate Partial Mocks in Rhino Mocks. So I’m going to re-implement my class methods. Public class MyClass { public int Foo() { // stuff happens here int i = Bar(); // do more stuff here return i + 5; } public virtual int Bar() { // do some logic return 1; } }

Why and what is Arrange Act Assert? - Arrange Act Assert. Many years ago when I first started writing unit tests, I always got to a point where my tests started to look messy.

Why and what is Arrange Act Assert? - Arrange Act Assert

When I say messy what I mean is it could take longer than it should to understand what was actually going on in a testwhat data was being usedwhat was being testedwhat the assertions were The problem is worse if you were looking at a test you didn’t write! I wasn’t the only one with this problem. Many examples in books, blogs and sample code had the same issue. In 2003 William C. From that moment on it was obvious to me that this was the only way to create tests that were easy to readfollowunderstand maintain It doesn’t matter if you’re someone starting out or a seasoned pro, if you start your unit tests with these three comments, it will help you structure your tests in a way that will allow you to focus on writing better unit tests. Here’s an example to show what I mean.

If we wanted to test two numbers can be added together, we could write something like this.

Rhino Mock

Visual Studio. How to: Create a Data-Driven Unit Test. As an example, let's assume that we have created: A solution called MyBank that accepts and processes transactions for different types of accounts.

How to: Create a Data-Driven Unit Test

A project in MyBank called BankDb that manages the transactions for accounts. A class called Maths in the DbBank project that performs the mathematical functions to ensure that any transaction is advantageous to the bank. A unit test project called BankDbTests to test the behavior of the BankDb component. A unit test class called MathsTests to verify the behavior of the Maths class. We will test a method in Maths that adds two integers using a loop: public int AddIntegers(int first, int second) { int sum = first; for( int i = 0; i < second; i++) { sum += 1; } return sum; } To test the AddIntegers method, we create a data source that specifies a range of values for the parameters and the sum that you expect to be returned.

Data Driven test - embedded XML. C# - Data driven tests using nested xml data file.

Basics

Übersicht über datengesteuerte Komponententests. Aktualisiert: November 2007 Ein datengesteuerter Komponententest ist ein Komponententest, der für jede Zeile einer Datenquelle wiederholt ausgeführt wird. Ein häufiges Szenario für das Verwenden von datengesteuerten Komponententests ist das Verwenden mehrerer Eingabewerte zum Testen einer API-Funktion. Anstatt mehrere Komponententests zu erstellen, die die API mit jeweils neuen Eingaben aufrufen, oder ein Array in einem Komponententest anzulegen und Schleifencode zu verwenden, können Sie eine einzelne Komponententestmethode verfassen, um die API einer Prüfung zu unterziehen. Sie können dann Daten aus den Zeilen einer Datenbanktabelle abrufen, um sie z. B. wiederholten Aufrufen der Testmethode zu übergeben. Sie können diese Methode zum Testen von Anwendungen verwenden, die von mehreren Benutzern mit jeweils eigenen Rollen eingesetzt werden.

Anzeigen von Testergebnissen für datengesteuerte Komponententests Siehe auch Aufgaben Konzepte Referenz Weitere Ressourcen. Arbeiten mit Komponententests. Aktualisiert: November 2007 Beschreibt Aspekte der Arbeit mit Tests, darunter das Anpassen und Filtern von Testansichten, die Arbeit mit Testlisten und die Verknüpfung von Tests mit Arbeitsaufgaben.

Arbeiten mit Komponententests

Beschreibt viele Bereiche der Testausführung, einschließlich der Konfiguration von Testdurchläufen, der Ausführung von Tests in der Visual Studio-IDE und über die Befehlszeile sowie des Debuggens bei der Testausführung. Beschreibt Testergebnisse und verschiedene Arten, mit diesen zu arbeiten, darunter das Anzeigen, Speichern und Veröffentlichen dieser Ergebnisse sowie das Erstellen von Fehlern (Bugs). Beschreibt das Erstellen, Bearbeiten, Ausführen und Anzeigen von Webtests.