Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber.js property initialization explanation
    primarykey
    data
    text
    <p>I'm just getting started with Ember and come across this issue while writing some jasmine tests.</p> <p>Given I have the following code</p> <pre><code>App.RecipeController = Ember.Controller.extend selectedGrain: null amount: null recipeGrains: Ember.A() totalWeight: (-&gt; weight = 0 @get('recipeGrains').forEach (grain) -&gt; weight += grain.get('weight') weight ).property('recipeGrains.@each') addGrain: -&gt; grain = Ember.Object.create name: @get('selectedGrain').get('name') weight: parseFloat(@get('amount')) @get('recipeGrains').pushObject(grain) @set('selectedGrain', null) @set('amount', null) </code></pre> <p>And I write the following test.</p> <pre><code>describe("Controllers", function() { describe("NewRecipeController", function() { var controller; beforeEach(function() { controller = Brewery.NewRecipeController.create(); }); it("calculates the correct total weight", function() { var grains = controller.get('recipeGrains'); grains.pushObject(Ember.Object.create({weight: 4.0})); grains.pushObject(Ember.Object.create({weight: 3.2})); expect(controller.get('totalWeight')).toEqual(7.2); }); it ("adds grains based on its selected grain", function() { controller.set('selectedGrain', Ember.Object.create({name: "Wheat"})); controller.set('amount', '10.2'); controller.addGrain(); expect(controller.get('totalWeight')).toEqual(10.2); }); }); }); </code></pre> <p>I expected that both tests would pass, but instead the second test fails with the message </p> <blockquote> <p>Expected 17.4 to equal 10.2.</p> </blockquote> <p>It seems the state of the first test is spilling over the the second test. Can someone more knowledgeable than I pelase explain how Ember handles controller states and why this happens?</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload