Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 利用rails 4设计Rspec登录测试_Ruby On Rails_Ruby On Rails 3_Rspec_Devise_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 利用rails 4设计Rspec登录测试

Ruby on rails 利用rails 4设计Rspec登录测试,ruby-on-rails,ruby-on-rails-3,rspec,devise,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Rspec,Devise,Ruby On Rails 4,在我的身份验证规范中,我有 require 'spec_helper' describe "Authentication" do subject { page } describe "Signup page" do before { visit new_user_registration_path } let(:submit) { "Create my account" } describe "with invalid i

在我的身份验证规范中,我有

require 'spec_helper'

describe "Authentication" do

    subject { page }

    describe "Signup page" do
        before { visit new_user_registration_path }

        let(:submit) { "Create my account" }

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

          describe "after submission" do
            before { click_button submit }

            it { should have_content('Sign up') }
            it { should have_content('error') }
          end
        end

        describe "with valid information" do
          before do
            fill_in "First name",   with: "Example"
            fill_in "Last name",    with: "Lastname"
            fill_in "Username",     with: "exampleguy"
            fill_in "Email",        with: "user@example.com"
            fill_in "Password",     with: "foobar1234"
          end

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

          #describe "after saving the user" do
            #before { click_button submit }
            #let(:user) { User.find_by(email: 'user@example.com') }

            #it { should have_title(user.first_name) }
            #it { should have_selector('div.flash_success', text: 'Welcome') }
         # end
        end

        it { should have_content('Sign up') }
    end


    describe "Signin page" do 

        before { visit new_user_session_path }

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

            it { should have_content('Sign in') }
            it { should have_selector('div.flash_alert', text: "Invalid") }

        end

        describe "with valid information" do
            let(:user) { FactoryGirl.create(:user) }
            before do 
                fill_in "Email",    with: user.email
                fill_in "Password", with: user.password
                click_button "Sign in"
            end

            it { should_not have_selector('a', text: 'Sign up')}
            it { should_not have_selector('a', text: 'Sign in')}
            it { should have_selector('a', text: 'Profile') }
            it { should have_selector('a', text: 'Sign Out') }
            it { should have_selector('div.flash_notice', text: "Signed in successfully.") }

        end

        it { should have_content('Sign in') }
    end
end
在工厂里

FactoryGirl.define do
  factory :user do
    first_name  "Example"
    last_name   "User"
    username    "exampleuser"
    email       "user@example.com"
    password    "foobar1234"
  end
end

除了登录之外,每个测试都在工作-使用有效的测试信息集。我不知道为什么,它在浏览器中正常工作。有人遇到过这样的问题吗?

如果您使用的是Desive confirmable模块,则必须添加user.confirm!到您的登录方法。
将其放在support/utilities.rb中

def sign_in(user)
   user.confirm!
   visit new_user_session_path
   fill_in "Email",    with: user.email
   fill_in "Password", with: user.password
   click_button "Sign in"
end
在“使用有效信息”描述块中,更改此选项

before do 
   fill_in "Email",    with: user.email
   fill_in "Password", with: user.password
   click_button "Sign in"
end
对这个

before { sign_in(user) }

如果您正在使用Desive可确认模块,则必须添加user.confirm!到您的登录方法。
将其放在support/utilities.rb中

def sign_in(user)
   user.confirm!
   visit new_user_session_path
   fill_in "Email",    with: user.email
   fill_in "Password", with: user.password
   click_button "Sign in"
end
在“使用有效信息”描述块中,更改此选项

before do 
   fill_in "Email",    with: user.email
   fill_in "Password", with: user.password
   click_button "Sign in"
end
对这个

before { sign_in(user) }

你能添加失败的行吗?是的,那些在登录页面->中具有有效信息的行,例如它{不应该有{u选择器('a',text:'Sign up')}和该测试组中的其他行。你能添加失败的行吗?是的,那些在登录页面->中具有有效信息的行,例如它{不应该有{u选择器('a',text:'Sign up')}还有测试组中的其他人。与其说是
support/utilities.rb
,不如说是
spec/support/utilities.rb
?Yh,你明白我想说的。与其说是
support/utilities.rb
,不如说是
spec/support/utilities.rb
?Yh,你明白我想说的。