1

我是 Ruby 和 Rails 的新手,我正在尝试以正确的方式实现测试。我目前正在努力让用户注册。我希望第一个用户注册成为管理员,然后每个用户都成为普通用户。

我目前在想我需要把我的测试写成一个特性,但我想知道这是否真的应该是一个模型测试。

我当前的代码就在这里

require 'spec_helper'

describe "User pages" do
    subject { page }

    describe "Sign up page" do
        before { visit signup_path }

        it { should have_button('Create!') }
    end

    describe "Creating an account" do
        before { visit signup_path }
        let(:submit) { "Create!" }

    describe "with invalid information" do
        it "should not create the user account" do
            expect { click_button "Create!"}.not_to change(User, :count)
        end

        describe "it should display errors" do
            before { click_button submit }

            it { should have_content('Failed signup') }
        end
    end

    describe "with valid information" do
        before do
            fill_in "Name", with: "Example User"
            fill_in "Email", with: "user@example.com"
            fill_in "Password", with: "password"
            fill_in "Confirm Password", with: "password"
        end

        it "should create the user" do
            expect { click_button submit }.to change(User, :count).by(1)
        end

        # User sign in not implemented at this point
        describe "the first user" do
            User.all.count.should == 0
            click_button submit
            User.all.count.should == 1
            @firstUser = User.first
            @firstUser.is_admin?.should == true

            describe "the second user" do
                before do
                    visit signup_path
                    fill_in "Name", with: "Example User2"
                    fill_in "Email", with: "user2@example.com"
                    fill_in "Password", with: "password"
                    fill_in "Confirm Password", with: "password"
                end

                click_button submit
                @secondUser = User.all.last
                @secondUser.is_admin?.should == false
            end
        end
    end
end

结尾

我最关心的是“有有效信息”部分,并想清理它,以便它正确地适合 rspec/capybara。

4

1 回答 1

0

你可以试试 michael hartl 的这个很棒的教程,他在本教程中使用 Rspec,你可以从中学习 :) 我也是初学者,我从它开始 :D

于 2013-07-12T02:05:02.453 回答