1

基本的に Twitter のレプリカを作成するために、Michael Hartl の Rails チュートリアル アプリに取り組んできました。テスト スイートで問題が発生しています。私のコードと Hartl のコードの違いの 1 つは、テスト ユーザーを生成するために FactoryGirl をインストールする気がなかったので、テストの最初にユーザーを作成することです。これは問題の一部であると思われますが、正確に何が間違っているのかを理解したいと思います。以下の私のテストスクリプト(次の行でエラーが発生しています:fill_in "Email"、with:user.email):

describe "Authentication" do 
subject {page}
before do
    @user = User.create(name: "Example User", email: "user@example.com", 
          password: "foobar", password_confirmation: "foobar")
end

describe "signin page" do
    before { visit signin_path }

    describe "with invalid information" do
        before { click_button "Sign in"}

        it { should have_selector('title', text: 'Sign in')}
        it { should have_selector('div.alert.alert-error', text: 'Invalid')}

    end



    describe "with valid information" do
        let(:user) { User.find_by_email(@user.email) }

        fill_in "Email", with: user.email
        fill_in "Password", with: user.password
        click_button "Sign in"
    end

    it { should have_selector('title', text: user.name) }
    it { should have_link('Profile', href: user_path(user)) }
    it { should have_link('Sign out', href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
    it { should have_link('Sign out')}


    describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
    end
end

終わり

更新:自分で解決することができました-有効な情報ブロックを使用して記述内にユーザーインスタンスを作成し、let(:user) メソッドを使用する代わりに、作成したインスタンスを直接参照することで解決しました。

'describe "with valid information" do before do @user = User.create(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")

        fill_in "Email", with: @user.email
        fill_in "Password", with: @user.password
        click_button "Sign in"
    end


    it { should have_selector('title', text: @user.name) }
    it { should have_link('Profile', href: user_path(@user)) }
    it { should have_link('Sign out', href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
    it { should have_link('Sign out')}
end'
4

2 に答える 2

2

itブロックするためにこの行をラップしませんでした。例えば

 it "can sign up" do
   fill_in "Email", with: user.email
   fill_in "Password", with: user.password
   click_button "Sign in"
 end
于 2012-11-28T02:35:31.567 に答える
1

あなたがすべきことは次のとおりだと思います:

describe "Authentication" do 
  subject { page }

  let!(:user) do
    User.create(name: "Example User", email: "user@example.com", 
      password: "foobar", password_confirmation: "foobar")
  end

  describe "signin page" do
    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in"}

      it { should have_selector('title', text: 'Sign in')}
      it { should have_selector('div.alert.alert-error', text: 'Invalid')}
    end

    describe "with valid information" do
      fill_in "Email", with: user.email
      fill_in "Password", with: user.password
      click_button "Sign in"
    end

    it { should have_selector('title', text: user.name) }
    it { should have_link('Profile', href: user_path(user)) }
    it { should have_link('Sign out', href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
    it { should have_link('Sign out')}

    describe "after visiting another page" do
      before { click_link "Home" }

      it { should_not have_selector('div.alert.alert-error') }
    end
  end
end

let!アクセサーをインスタンス化し、ブロックuserに入れbefore、すべての例の後にリセットするのは何ですか。

于 2012-11-29T08:49:20.387 に答える