开发者

How to spec controller where model associations occur

I'm trying to spec the controller code:

# ClustersController
def create
  # create new cluster
  @cluster.user = current开发者_如何学C_user
  # save code
end

I am using Rails 3 / RSpec 2 and I'm fairly new to the TDD flow. I basically want to make sure that the user attribute is assigned during the create action.


To begin with i don't think you should create, update an save the object. You can pass the user to the create method, like this:

Cluster.create(:user => current_user)

And to test this you can do:

describe ClusterController do
  describe "POST create" do
    it "creates a new cluster" do
      lamda do 
        post :create
      end.should change(Cluster, :count).by(1)
    end

    it "set the current user as the new cluster's user" do
      user = mock()
      Cluster.should_receive(:create).with(:user => user)
      post :create
      assign(:cluster).user.should == user
    end
  end
end

I think that will do.

Hope that help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜