An example of testing a module in a class with required fields?
I am new to Unit test development and I cannot find an example on how to test an existing class I have.
The class has a save method that inserts or updates in the database when the user clicks the Save button in the user interface. But the save method requires fields to be filled in. And there are other fields that don't.
So how can I run this test correctly?
tried to write it down.
Give a user
When user saves object
Then Field1 is required
then Field2 is required
Then Field3 is required
WhenUserSavesObject()
object = new object
object.field1 IsNot Nothing
something like that?
What about other fields that are optional? How can I test the save method to make sure it accepts all these values correctly?
tried to use BDD but not sure if I should try or not.
There is no way to find examples of classes with many properties that are needed when calling a test method.
a source to share
You have not specified what language you want to implement your project and your tests in. For example, with Groovy and the Grails framework, it provides a wonderful set of tools for running these tests and works great as an example of how to run tests like the ones you're asking for.
void testUsernameIsRequiredToValidate() {
mockForConstraintsTests User
def user = new User(username: "")
assertFalse user.validate()
assertEquals "blank", user.errors["username"]
}
The code is on its own. In the first line of the test method, we create an environment to add test functions to the class User
in the unit test. After that we create a new user and check it for restrictions. The last line verifies that the a- blank
contrast in the field firstname
caused the test to fail.
class User {
String firstname
String password
static constraints = {
username(blank: false)
password(blank: false, minSize: 6)
}
}
The presence of several required fields for testing an object User
can be implemented in several small classes for negative testing. For positive testing, you will create a valid instance User
and validate it.
void testUserWithUsernameAndAMinimumPasswordOfSixCharactersIsValid() {
mockForConstraintsTests User
def user = new User(username: "admin", password: "secret")
assertTrue user.validate()
}
If you are developing a different framework and / or language, you may be interested in the topic of mocking dependencies and breaking down testable code into small testable parts.
a source to share
If you are just learning about unit tests, IMHO you can choose a simpler target for your first unit test. Unit testing with a database is much more difficult than testing a simple object with no dependencies. Better to practice and learn one thing at a time.
However, if you really want to fix this problem, you have to tell us which language, save mechanism you are using the DB - otherwise we just shoot in the dark. For instance. if you are using Java DbUnit is your friend.
It's generally a good idea to limit your database dependencies down to a thin layer within your code to make the rest of your code easier to test. It is not clear from your description if your class is already part of this layer or if it may be isolated from the DB yet.
a source to share