2012-09-22 31 views
2

我已經添加了一個非常簡單的計數器到我的發佈模型。每當有人開個帖子,它由1與rspec測試頁面查看計數器

def show 
    @story = Story.find(params[:id]) 
    @comment = @story.comments.build 
    @comments = @story.comments.arrange(order: :created_at) 

    @story.increment!(:counter) # ---Here--- 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @story } 
    end 
    end 

更新相應的字段:counter其實,我查了DB這個東西是工作。但是,我的測試得到失敗:

it "raise the counter by one when visiting a page" do 
    story = FactoryGirl.create(:story) 
    expect { 
    visit story_url(story) 
    }.to change { story.counter }.by(1) 
end 

它表明:

1) Stories raise the counter by one when visiting a page 
    Failure/Error: expect { 
    result should have been changed by 1, but was changed by 0 
    # ./spec/requests/stories_spec.rb:8:in `block (2 levels) in <top (required) >' 

這裏是我的test.log中:

Started POST "/stories" for 127.0.0.1 at 2012-09-22 11:31:32 +0330 
Processing by StoriesController#create as HTML 
    Parameters: {"utf8"=>"✓", "story"=>{"title"=>"sfomqd8s2r", "content"=>"Beatae voluptatibus quia cumque voluptate. Beatae sed aut sit. Fugiat deleniti et vitae accusantium blanditiis. Consequatur at itaque temporibus autem. Quo beatae delectus minus porro et.", "tag_names"=>""}, "commit"=>"submit"} 
    (0.1ms) SELECT MIN("tags"."stories_count") AS min_id FROM "tags" 
    (0.1ms) SELECT MAX("tags"."stories_count") AS max_id FROM "tags" 
    Tag Load (0.1ms) SELECT name, stories_count FROM "tags" ORDER BY name asc 
    (0.1ms) SAVEPOINT active_record_1 
    SQL (1.5ms) INSERT INTO "stories" ("comments_count", "content", "counter", "created_at", "publish_date", "slug", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["comments_count", 0], ["content", "Beatae voluptatibus quia cumque voluptate. Beatae sed aut sit. Fugiat deleniti et vitae accusantium blanditiis. Consequatur at itaque temporibus autem. Quo beatae delectus minus porro et."], ["counter", 0], ["created_at", Sat, 22 Sep 2012 11:31:32 IRST +03:30], ["publish_date", nil], ["slug", nil], ["title", "sfomqd8s2r"], ["updated_at", Sat, 22 Sep 2012 11:31:32 IRST +03:30], ["user_id", nil]] 
    (0.1ms) RELEASE SAVEPOINT active_record_1 
Redirected to http://www.example.com/stories/2 
Completed 302 Found in 18ms (ActiveRecord: 1.9ms) 


Started GET "/stories/2" for 127.0.0.1 at 2012-09-22 11:31:32 +0330 
Processing by StoriesController#show as HTML 
    Parameters: {"id"=>"2"} 
    (0.1ms) SELECT MIN("tags"."stories_count") AS min_id FROM "tags" 
    (0.1ms) SELECT MAX("tags"."stories_count") AS max_id FROM "tags" 
    Tag Load (0.1ms) SELECT name, stories_count FROM "tags" ORDER BY name asc 
    Story Load (0.1ms) SELECT "stories".* FROM "stories" WHERE "stories"."id" = ? LIMIT 1 [["id", "2"]] 
    CACHE (0.0ms) SELECT "stories".* FROM "stories" WHERE "stories"."id" = ? LIMIT 1 [["id", "2"]] 
    Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."story_id" = 2 ORDER BY (case when comments.ancestry is null then 0 else 1 end), comments.ancestry, created_at 
    SQL (0.3ms) UPDATE "stories" SET "counter" = 1 WHERE "stories"."id" = 2 
    Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."story_id" = 2 
    Rendered stories/_story_operation.html.erb (3.5ms) 
    Rendered stories/_story.html.erb (5.4ms) 
    Rendered comments/_form.html.erb (7.1ms) 
Completed 200 OK in 34ms (Views: 18.1ms | ActiveRecord: 1.1ms | Solr: 0.0ms) 
    (0.7ms) rollback transaction 

發生了什麼我不明白。也許這是關於水豚或我寫測試的方式。

回答

2

如你所知,

expect { 
    visit story_url(story) 
}.to change { story.counter }.by(1) 

之前並執行塊後story.counter的值進行比較。當你的控制器加載和修改故事時,這是一個完全獨立的ruby對象(碰巧指的是數據庫中的同一行):規範代碼中的story對象不知道對數據庫所做的更改並保持不變(現在陳舊)的數據。

爲了讓你的規範通過,你可以改爲檢查story.reload.counter是否在改變。

+0

嗯......謝謝:) – ArashM