1

我对 rspec 2.14 语法有疑问。RSpec 控制器工作得很好,但它需要不同的语法。

describe Frontend::UsersController, type: :controller do
  describe 'POST "create"' do
    subject { post :create, user: { login: email } }

    context 'with valid attributes' do
      let(:email) { FactoryGirl.attributes_for(:user)[:email] }

      it { expect{ subject }.to change{ User.count }.by(1) }
      it { expect(subject).to redirect_to(root_path) }

为什么更改和重定向方法需要不同的语法?

4

1 回答 1

1

They don't. You could do change(User, :count) instead. The form you're using evaluates the block before and after running that line and checks if the value changed appropriately. In English:

  • The User count is X right now.
  • Call create in the Frontend::UsersController.
  • The User count is Y after that.
  • I expect Y to equal X + 1.

UPDATE

In case you're actually talking about expect{subject} vs. expect(subject): the change expectation needs something to test for change against. Since you're passing expect a block, change knows it can first check the User count, evaluate the block (call subject), then check the User count again. If you didn't pass a block, it's ambiguous for when you'd actually want to start checking for changes in the User count.

于 2013-11-05T05:24:39.443 回答