ruby on rails - FactoryGirl doesn't save objects in DB -


i have code in spec/requests:

      describe "with valid data"         let(:player) { factorygirl.create :player }         let(:player2) { factorygirl.create :player2 }           @player = player.new(firstname: "tom", lastname: "greg", nickname: "jimi21")         @player.save          @players = player.all         p @players.count       end 

output in console is:

1

why code don't create object in test db? mean line:

let(:player) { factorygirl.create :player } 

i want write more tests based on players saved in db. how can smartly using rspec , factorygirl?

code factorygirl file:

factorygirl.define   factory :player     firstname "john"     lastname "smith"     nickname "smithy99"     avatar { file.new(rails.root.join('spec/images/1.png')) }   end    factory :player2     firstname "tom"     lastname "marlow"     nickname "marlowy11"     avatar { file.new(rails.root.join('spec/images/1.png')) }   end end 

you don't have tests in code there, , you're never referencing variables you're setting in them, let blocks never being run.

tests in rspec wrapped in it blocks, eg.

it "tests something"   # test! player defined here! end 

if set test this, , reference player , player2 inside it, defined.

if don't wish manually reference them them defined, may want eager load them, using let! instead of let.

for complete snippet of code think want:

  describe "with valid data"     let!(:player) { factorygirl.create :player }     let!(:player2) { factorygirl.create :player2 }       "doesn't allow duplicate names"       @player = player.new(firstname: "tom", lastname: "greg", nickname: "jimi21")       @player.save        @players = player.all       p @players.count     end   end 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -