Say I have a class in Coffeescript:
class MyGame
constructor: () ->
#me = new Player
#opponents = [new Player, new Player]
which would like to test in Jasmine:
describe "MyGame", ->
beforeEach ->
window.game = new MyGame
it "should have two players", ->
expect(window.game.opponents.length).toEqual 2
But I get the error TypeError: Result of expression 'window.game.opponents' [undefined] is not an object.?
The window.game approach also seem awkward to me. If I try to define it as #game = new MyGame I get the error ReferenceError: Can't find variable: MyGame but I guess that has something to do with the way Coffeescript is wrapping things up?
UPDATE: The problem seems more like a reference problem as described above. I'm running with guard-jasmine which looks like
guard 'jasmine', :all_on_start => false, :all_after_pass => false do
watch(%r{app/assets/javascripts/(.+)\.(js\.coffee|js)}) { |m| "spec/javascripts/#{m[1]}_spec.#{m[2]}" }
watch(%r{spec/javascripts/(.+)_spec\.(js\.coffee|js)}) { |m| "spec/javascripts/#{m[1]}_spec.#{m[2]}" }
watch(%r{spec/javascripts/spec\.(js\.coffee|js)}) { "spec/javascripts" }
end
and my jasmine.yml file has:
src_files:
- "app/assets/**/*.js"
- "app/assets/**/*.coffee"
spec_files:
- '**/*[sS]pec.js.coffee'
asset_pipeline_paths:
- app/assets
- spec/javascripts
I get the an ReferenceError: Can't find variable: MyGame so I figure it's either something with the Rails 3.1 asset pipeline or the way Coffeescript wraps objects.
try defining your coffeescript class using the # operator as such:
class #MyGame
constructor: () ->
#me = new Player
#opponents = [new Player, new Player]
this will allow you to access the class from anywhere, such as from your jasmine tests, and also you can get away from attaching testing variables to window:
describe "MyGame", ->
beforeEach ->
#game = new MyGame
it "should have two players", ->
expect(#game.opponents.length).toEqual 2
the reason for this is that coffeescript goes out of its way to avoid introducing global variables by wrapping everything in a closure. unfortunately, this can be undesirable for object-oriented code. using the # operator attaches the class definition to the global this, which is window, and thus allows you to instantiate your classes as you like. you may have some global vars in your global space now, your classes, but for me its an ok trade-off. hope this helps!
I wasn't willing to accept modifying the namespace of my code by using an # symbol in front of all my backbone classes, so I dug around some more and the solution that worked for me was to require the application file in my spec/javascripts/spec.js.coffee file
#= require application
window.game = () -> new MyGame
This will assign a function that returns a new MyGame to window.game. Did you not just want the new instance directly?
window.game = new MyGame
The window.game approach also seem awkward to me.
How about this
describe "MyGame", ->
game = null
beforeEach ->
game = new MyGame
it "should have two players", ->
expect(game.opponents.length).toEqual 2
I have solved the problem by defining every class as class window.MyGame for example. In the spec files I put #= require my_file_name in the top.
Furthermore I have placed both jasminerice.js.coffee and jquery.js in app/assets/javascripts. This might not be the best solution as I assume they should be placed in spec/javascripts/helpers as my spec.js.coffee's content is #=require_tree ./.
I'm aware that this is not very elegant but it might help others in the same situation. #Thilo thanks for your inputs.
Related
I recently switched to Java 11 for a rather big project, and would like to switch to using var class = new Class() instead of Class class = new CLass().
I tried using Intellij Structural Search (and replace) for this, but its turning out to be more complex than expected.
My first attempt was $Type$ $Inst$ = new $Constructor$($Argument$);, which also matches global variables (which don't allow var).
My second attempt is:
class $Class$ {
$ReturnType$ $Method$ ($ParameterType$ $Parameter$) throws $ExceptionType$ {
$Statements$;
final $Type$ $Inst$ = new $Constructor$($Argument$);
$Statements2$;
}
}
Which misses all calls inside e.g. try blocks (since they get matched by the expressions)
Any help would be greatly appreciated!
Use your first template
$Type$ $Inst$ = new $Constructor$($Argument$);
But add a Script modifier on the $Inst$ variable with the following text:
Inst instanceof com.intellij.psi.PsiLocalVariable
Alternatively you may want to try the Local variable type can be omitted inspection that is available in IntelliJ IDEA.
I'm new to unit / integration testing and I want to do an integration test of my controller which looks simplified like this:
// ItemsController.php
public function edit() {
// some edited item
$itemEntity
// some keywords
$keywordEntities = [keyword1, keyword2, ...]
// save item entity
if (!$this->Items->save($itemEntity)) {
// do some error handling
}
// add/replace item's keywords
if (!$this->Items->Keywords->replaceLinks($itemEntity, $keywordEntities)) {
// do some error handling
}
}
I have the models Items and Keywords where Items belongsToMany Keywords. I want to test the error handling parts of the controller. So I have to mock the save() and replaceLinks() methods that they will return false.
My integration test looks like this:
// ItemsControllerTest.php
public function testEdit() {
// mock save method
$model = $this->getMockForModel('Items', ['save']);
$model->expects($this->any())->method('save')->will($this->returnValue(false));
// call the edit method of the controller and do some assertions...
}
This is working fine for the save() method. But it is not working for the replaceLinks() method. Obviously because it is not part of the model.
I've also tried something like this:
$method = $this->getMockBuilder(BelongsToMany::class)
->setConstructorArgs([
'Keywords', [
'foreignKey' => 'item_id',
'targetForeignKey' => 'keyword_id',
'joinTable' => 'items_keywords'
]
])
->setMethods(['replaceLinks'])
->getMock();
$method->expects($this->any())->method('replaceLinks')->will($this->returnValue(false));
But this is also not working. Any hints for mocking the replaceLinks() method?
When doing controller tests, I usually try to mock as less as possible, personally if I want to test error handling in controllers, I try to trigger actual errors, for example by providing data that fails application/validation rules. If that is a viable option, then you might want to give it a try.
That being said, mocking the association's method should work the way as shown in your example, but you'd also need to replace the actual association object with your mock, because unlike models, associations do not have a global registry in which the mocks could be placed (that's what getMockForModel() will do for you) so that your application code would use them without further intervention.
Something like this should do it:
$KeywordsAssociationMock = $this
->getMockBuilder(BelongsToMany::class) /* ... */;
$associations = $this
->getTableLocator()
->get('Items')
->associations();
$associations->add('Keywords', $KeywordsAssociationMock);
This would modify the Items table object in the table registry, and replace (the association collection's add() acts more like a setter, ie it overwrites) its actual Keywords association with the mocked one. If you'd use that together with mocking Items, then you must ensure that the Items mock is created in beforehand, as otherwise the table retrieved in the above example would not be the mocked one!
I have a problem with the "this" reference on Backbone with Coffeescript, this is a method which shows information of an artist:
show: (id) ->
self = #
if #collection
artist = #collection.get(id)
#renderArtist(artist)
else
artist = new DemoBackbone.Models.Artist({id: id})
artist.fetch
success: ->
self.renderArtist(artist)
renderArtist: (artist) ->
view = new DemoBackbone.Views.ArtistsShow(model: artist)
$('#content_artists').html(view.render().el)
This works perfectly, but I'm using the "self = #" statement so I can use the Class function "renderArtist", but is there a more "elegant" way to do this on "success: -> self.renderArtist(artist)", so I can avoid using the "self = #" line??
Something like
success: #->
#renderArtist(artist)
I'm not pretty sure but I think there should be a way to do this.
Thanks
This is exactly what the double-arrow function in CoffeeScript does. It will generate code like you have shown for you automatically so you don't have to write it yourself.
success: =>
#renderArtist(artist)
I'm trying to use a single controller to list multiple similar collections so I can call different templates with the same controller. In fact, right now I have 6 controllers for listing and another 6 for forms but they're all duplicates.
I've made a non-functional plunker just to show how I intend it to work. I've avoided declaring routeProviders because knowing it wouldn't work I tried to make it as straight to the point as I could.
http://plnkr.co/edit/d06PcrJS5newhrmNy6EJ?p=preview
I've seen on stackoverflow how to declare a class with a dynamic name:
var str = "MyClass";
var obj = new window[str];
But as I have not been able to find where it's stored I'm not able to retrieve it.
Does anyone have a hint on how to do this?
You can use Angular's injector to return the service instance you want. For example:
app.controller('NodeListCtrl', function($scope, $location, $injector) {
var modelName = $location.path().split("/")[1];
$scope.modelName = modelName.charAt(0).toUpperCase() + modelName.slice(1);
$scope.nodes = $injector.get($scope.modelName).query();
});
Note: Don't forget to add the $injector to the controller's function signature.
jsfiddle: http://jsfiddle.net/bmleite/Mvk2y/
I am using RhinoMocks 3.6 and would like to use the multimock feature to implement both a class and a interface.
var mocks = new MockRepository();
var project = mocks.StrictMultiMock(
typeof(Project),
typeof(INotifyCollectionChanged));
using (mocks.Record())
{
((INotifyCollectionChanged)project).CollectionChanged += null;
LastCall.Constraints(Is.NotNull()).Repeat.Any();
}
The LastCall is working though. I get this message :
System.InvalidOperationException : Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).
What am I doing wrong here??
Have you actually checked that the Project class has methods you can override as the error message indicates? I'll assume you have. :-)
I'd suggest you switch to using the AAA syntax instead of record/replay as shown here:
I assume you're wanting to know if the class under test reacts the right way when the CollectionChanged event is fired? If that's the case, you can do it something like this:
var project = MockRepository.GenerateMock<Project, INotifyPropertyChanged>();
project.Expect(p => p.SomeMethod())
.Repeat.Any()
.Raise(p => ((INotifyCollectionChanged)p).CollectionChanged += null,p,new NotifyCollectionChangedEventArgs());