Regarding why mocking your GetByProperty() doesn't work it sounds like more of a question for the Moq communit. I don't know it at least.
As for mocking the ISession itself I suspect it will lead to much troubles due to too much work required to return the proper things for each query. Besides, it won't actually verify if your queries work with _NHibernate_ or not. In my experience at least it's better to mock away the whole method that implements the query (i.e. GetProperty() in this case), and then separately test the queries by having tests that run with full NHibernate and an actual database. Either as full system tests or if you do "unit tests" than runs just the queries, but with a database connected. /Oskar 2016-08-02 23:22 GMT+01:00 <[email protected]>: > Hi, I am writing the Unit tests against the MVC application, which is > based on Nhibernate. I have successfully Moq ed some repository classes / > functions in order to test my service layer methods. But I came to this > function below in my repository and I don't know how to Moq it. I need to > Moq this function as many other functions rely on it. The function: > > public T GetByProperty<T>(string property, object value) > { > return (T) > this.Session.CreateCriteria(typeof(T)) > .Add(NHibernate.Criterion.Restrictions.Eq(property, value)) > > .SetCacheable(RepositoryFactory.CanCache<T>()).SetCacheMode(CacheMode.Normal) > .UniqueResult(); > } > > > I've never worked with Nhibernate before and I am not sure whether I need > to Moq Session or somehow I can Moq the whole method or what? > > > I've tried this but it always returns null: > > > Mock mockRepository = new Mock<IRepository>(); > mockRepository.Setup(x => x.GetByProperty<Account>(“UserName”, “MyUserName > ”)) > > .Returns(new Account { UserName = “MyUserName” }); > > > > I appreciate for you suggestions, thanks. > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/nhusers. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/nhusers. For more options, visit https://groups.google.com/d/optout.
