I am using grails 1.3.7.
While writing the unit tests for my controller, I am getting errors as my action in the controller uses dynamic finder for example "findByName('ABC')".
I understand that I need to mock the dynamic finders as well and I tried that thing out.But unfortunately didnt work out.
I request you to please help me out with the exact mocking snippet I need to use.
Also, for all thses kinds of mocking things, please suggest some documentaion URLs which will have all these.
Also, In my grails app, I have associataion between three domains as "hasMany and belongsTo". So while writing unit tests for controllers, please suggest how exactly I need to mock these domain classes.
Please I request all of you to answer asap as I am stuck with these doubts.
Thanks in Advance,
and awaiting for positive answers.
Thanks
If name is a property of the domain, then the dynamic finders will "automatically" be mocked when you mock the domain. You should simply be able to do:
mockDomain(YourDomain)
At this point you will just have null returned, so create some instances if you want that query to return them:
YourDomain instance = new YourDomain(name: 'foo')
mockDomain(YourDomain, [instance])
then you can do:
assert instance == YourDomain.findByName('foo')
Anyone got an update for this question with Grails 4.0.0 and Spock? Because it doesn't seem to have working dynamic finders.
To mock a domain dynamic finder try:
def someDomainMock = new GrailsMock(SomeDomain)
someDomainMock.demand.static.findByName(1..1) { String name ->
[new SomeDomain(name: name + '_1'), new SomeDomain(name: name + '_2')]
}
Related
I am very new to writing unit test cases for python and need some help.
I have a Flask based application which internally calls another url over REST
Sample code
#sample.route('/testing', methods=["GET"])
def testing():
resp = requests.get("some url")
data = resp.json()
resp1 = requests.post("another url", data)
return resp1.status_code()
Now since for unit testing, my other module is not available yet and i need to write unit test cases for this module.
so i would need to mock these rest requests and return custom data and status code for each request.
Can anyone please help on how to proceed for this.
I tried various links online but not working as per my expectation.
You can mock, that is, create a custom mock-up for external APIs in a number of ways. In general, it will involve a fake API that will return pre-set values when a call is made.
Also, please consider that tight coupling between the two APIs, that might cause problems in the future. Try to reformulate your arquitecture so that it use looser coupling
I have been trying to test my ember application for 2 days with no success. There are examples out there that use fixtures but I was wondering if you might have an example of test scripts where RESTAdapter was used. I have tried using Fixtures for testing as you can see in the fiddle here : http://jsfiddle.net/deewen/u68Mx/1/, but that is also not working for me(sorry I couldn't find proper expect.js file).
it("Check analyticsRuns controller", function () {
//var controller = AS.__container__.lookup('controller:analyticsRunsIndex');
//the returns null too even though I have that controller
visit('/analytics')
.then(function() {
expect(find('*')).to.not.be(undefined);
});
});
Any suggestions for resource or read that could guide me on this would be highly appreciated. Thanks.
In general you don't want your test to hit a live API endpoint because at that point you're not only testing your application code, you're also testing both the functionality of the API and the state of any data that may (or may not) already be stored in the API service.
The Ember Data tests setup fake AJAX responses, much like you'd do with something like webmock on the server side. Something like that is probably the way to go.
https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/adapter/rest_adapter_test.js#L34-L55
I'm trying to write a suite of integration tests, where I have custom code which uses the Umbraco API. The Umbraco database lives in a SQL Server CE 4.0 database (*.sdf file) and I've managed to get that association working fine.
My problem looks to be dependancies in the Umbraco code. For example, I would like to create a new user for my tests, so I try:
var user = User.MakeNew("developer", "developer", "mypassword", "my.email#email.com", adminUserType);
Now as you can see, I have pass a user type which is an object. I've tried two separate ways to create the user type, both of which fail due to a null object exception:
var adminUserType = UserType.GetUserType(1);
var adminUserType2 = new UserType(1);
The problem is that in each case the UserType code calls it's Cache method which uses the HttpRuntime class, which naturally is null.
My question is this: Can someone suggest a way of writing integration tests against Umbraco code? Will I have to end up using a mocking framework such as TypeMock or JustMock?
Here are some resources that may help:
http://www.aaron-powell.com/unit-testing-with-umbraco
http://stream.umbraco.org/video/726639/aaron-powell-unit-testing
I have a number of simple controller classes that use Doctrine's entity manager to retrieve data and pass it to a view.
public function indexAction() {
$pages = $this->em->getRepository('Model_Page')->findAll();
$this->view->pages = $pages;
}
What exactly should we be testing here?
I could test routing on the action to ensure that's configured properly
I could potentially test that the appropriate view variables are being set, but this is cumbersome
The findAll() method should probably be in a repository layer which can be tested using mock data, but then this constitutes a different type of test and brings us back to the question of
What should we be testing as part of controller tests?
Controller holds core logic for your application. Though simple "index" controller actions don't have any specific functions, those that verify/actively use data and generate viewmodels have pretty much the most functionality of the system.
For example, consider login form. Whenever the login data is posted, controller should validate login/password and return: 1) to index page whenever logins are good. Show welcome,{user} text. 2) to the login page saying that login is not found in db. 3) to the login page saying that password is not found in db.
These three types of outputs make perfect test cases. You should validate that correct viewmodels/views are being sent back to the client with the appropriate actions.
You shouldn't look at a controller like at something mysterious. It's just another code piece, and it's tested as any other code - any complicated logic that gives business-value to the user should be tested.
Also, I'd recommend using acceptance testing with framework like Cucumber to generate meaningful test cases.
Probably the controller is the hardest thing to test, as it has many dependencies. In theory you should test it in full isolation, but as you already seen - it has no sense.
Probably you should start with functional or acceptance test. It tests your controller action in a whole. I agree with previous answer, that you should try acceptance testing tools. But Cucumber is for Ruby, for PHP you can try using Codeception. It makes tests simple and meaningful.
Also on a Codeception page there is an article on how to test sample controllers.
I'm writing some unit tests in my project and I have a datacontext dependency on the controller containing the methods I'd like to test.
I'm using Ninject to inject the dependency and Moq to create my mock datacontext. My DI makes use of an interface IDataContext which my dbml impliments and is used through out the injection process.
In my unit test I'm creating my mock datacontext as follows:
var mock = new Mock<IDataContext>();
var myController = new MyController(mock.Object);
This throws a Object reference not set to an instance of an object. exception on the second line whilst executing the datacontexts constructor.
I'm clearly missing a fundamental piece in setting this up however most of the Moq examples I've seen involve some kind of test against the mocked object using Setup().
Am I going about this the right way? Should I be creating a mock of my IDataContext interface or something else?
haha,
the answer came while i was reading through emad's blog on unit testing in ASP.Net MVC.
I'm guessing that you didn't add the connection string to the app.config of your test project right? :)
And that's the database dependency way because you're still not mocking the database end.
So if you want to do that, you need to google up for some codes, there are a few ways to do that.
i find these few references below to be quite useful, but since i don't really have a need to mock the database end, i'm just using my test DB server for now.
link