Is there a good way of testing during pregnancy to know if your child with have mental retardation?

Good prenatal care. Obstetricians will test for most of the detectable forms of mental retardation, such as down's syndrome, during their assessment of a woman's pregnancy. Tools such as ultrasound, amniocentesis, and choriovillous biopsy are quite useful, as is a full knowledge of the family history, .
Not for every cause. First and second trimester maternal blood screens, fetal ultrasounds and some specific tests for women > 35 are offered. Your OB needs to know any and all risk factors including a 3-generation medical/ mental health history including ethnicity, alcohol, tobacco, drug use, and your health, habits and history to know what else to order to detect those causes than can be found.
Possibilities. In some pregancies the doctor will recommend genetic testing to see if there is evidence of any concerning problems, including possible mental retardation. This is usually only recommended if there are certain risk factors involved with the pregnancy, the testing itself can have some risks, so it is not done routinely. Otherwise, for the large majority of pregnancies, no testing is needed.