Ich brauche Hilfe beim Erstellen von Assoziation zwischen Domain-Klassen. Ich habe drei Bereiche: Benutzer, Nachrichten, Kommentare. Ein Benutzer kann viele Kommentare zu vielen Nachrichten machen. Eine Nachricht kann viele Kommentare von vielen Benutzern enthalten.GORM ASSOCIATION ERKLÄRUNG
Ich habe ein großes Problem, diese Verbindung in GORM zu implementieren.
Ich möchte in der Lage sein, für einen Nutzer Kommentar zu einer Nachricht hinzuzufügen. Der Kommentar sollte einen Verweis auf den Benutzer haben, der ihn erstellt hat. Und eine Nachricht sollte eine Liste aller Kommentare enthalten.
Domain Klassen:
class User {
String nickName
String password
Date dateCreated
static hasMany = [users : User, comments : Comment]
static hasOne = [profile : Profile]
//static belongsTo = [comments : Comment]
static constraints = {
profile nullable: true
}
}
class News {
String webTitle
String webPublicationDate;
String trailText;
String webUrl;
String thumbnail;
String webId;
static hasMany = [tags : Tag, comments : Comment]
static constraints = {
}
}
class Comment {
//static belongsTo = [news : News, user : User]
String content
int likes
int disLikes
Date lastUpdated
static hasOne = [user: User, news: News]
static constraints = {
}
}
IntegrationTests umfassen:
void "A user can add to a post's comments"(){
given: "An existing user"
def newUser = new User(nickName: 'nickName', password: 'password').save(failOnError: true, flush : true)
and: "A comment and a news"
def news = new NewsItem(webTitle: 'I Love Hillary', webPublicationDate: new Date(),
trailText: 'Hillary can change America', webUrl: 'http:google.com', webId: '4', thumbnail: 'image')
when: 'A user adds comment to post'
def comm = new Comment(content: "Hillary cant be president", user: newUser, newsItem: news)
newUser.addToComments(comm)
news.addToComments(comm)
then: "NewsItem must have some comments"
news.comments.size() == 1
news.comments[0].user == newUser
newUser.comments.size() == 1
comm.user == newUser
news.comments != null
}
Jede Hilfe wird
„meine Tests versagen“ - wie sie sind Versagen? Bitte fügen Sie Ihre fehlgeschlagenen Behauptungen hinzu, Stacktraces, ... – cfrick
Ich habe das Testergebnis zur Verfügung gestellt. Danke –