Tag: rspec

Capybara-webkit无法处理与bootstrap glyphicon的链接

我有一个链接: link_to %q().html_safe, feed_item, data: {confirm: ‘Are you sure?’,toggle: ‘tooltip’}, method: :delete, title: ‘Delete’, id: ‘delete_post’ 和以下function规范代码 page.accept_confirm do click_link ‘delete_post’ end 我收到以下错误 Capybara::Webkit::ClickFailed: Failed to find position for element /html/body/div[@id=’wrapper’]/div[@id=’page-content-wrapper’]/div/div[@id=’main’]/div[@id=’main_content’]/div[4]/div/div/div[1]/h3/div/a[@id=’delete_post’] 如果我用一些文本(例如“删除”)用glyphicon替换span,则测试有效。 这似乎与Github上未解决的问题有关,可能与CSS覆盖可点击的html元素有关。 但是,我无法理解在这种情况下如何发生这种重叠或如何纠正它。 将图标用于“编辑”或“删除”等常见任务已成为一种常见做法,因此最好找到解决方案。 我该如何解决这个问题?

使用OmniAuth在Factory Girl中创建用户?

我目前正在创建一个使用OmniAuth创建和validation用户的应用程序。 我在测试期间遇到问题,因为Factory Girl无法在没有OmniAuth的情况下生成用户。 我有几种不同的方法让工厂女孩用omniauth创建用户,但没有一个成功。 我在spec_helper文件中添加了以下两行 OmniAuth.config.test_mode = true \\ allows me to fake signins OmniAuth.config.add_mock(:twitter, { :uid => ‘12345’, :info => { :nickname => ‘Joe Blow’ }}) 目前的工厂.rb FactoryGirl.define do factory :user do provider “twitter” sequence(:uid) { |n| “#{n}” } sequence(:name) { |n| “Person_#{n}” } end end 以下测试当前失败,因为没有生成用户 let(:user) { FactoryGirl.create(:user) } before { sign_in […]

如何在RSpec中模拟类方法期望语法?

我对如何使用新的expect语法模拟类方法感到困惑。 这有效: Facebook .should_receive(:profile) .with(“token”) .and_return({“name” => “Hello”, “id” => “14314141”, “email” => “hello@me.com”}) 这不是: facebook = double(“Facebook”) allow(facebook).to receive(:profile).with(“token”).and_return({“name” => “Hello”, “id” => “14314141”, “email” => “hello@me.com”}) 谁能告诉我这里有什么问题?

RSpec测试PUT更新动作

我正在尝试编写一些RSpec测试来测试我的应用程序,但我偶然发现了一些我找不到任何解决方案的问题。 1)我正在尝试测试更新操作。 这是我的代码: it “email is a new one” do put :update, id: @user, user: FactoryGirl.attributes_for(:user, :email=>”a@bc”) @user.reload @user.email.should == “a@bc” puts @user.email end 这是UsersController更新操作: def update @user = User.find(params[:id]) respond_to do |format| if @user.update_attributes(params[:user]) format.html { redirect_to edit_user_path(@user), :notice => “Your settings were successfully updated.”} format.json { head :no_content } else format.html { render […]

RSpec与Cookies – 即使应用程序正常工作(模拟已登录的用户),测试也会失败

我正在阅读Michael Hartl的“Ruby on Rails”Turorial“但我被困在这里。 我写了测试以检查用户编辑/更新的用户授权,我还编写了控制器代码, 它的工作原理如下: 如果用户未登录并尝试访问users#edit或users#update则会将其重定向到登录页面 如果用户已登录并且他尝试编辑另一个用户,则会将其重定向到根目录 问题是在测试第2点的规范中,我检查发送PUT请求到user_path我被重定向到root但运行rspec我得到以下错误: Failures: 1) Authentication authorization as wrong user submitting a PUT request to users#update Failure/Error: specify { response.should redirect_to(root_url) } Expected response to be a redirect to but was a redirect to # ./spec/requests/authentication_pages_spec.rb:73:in `block (5 levels) in ‘ 它似乎被重定向到signin_url而不是root_url ,就像用户根本没有登录一样……这就是我猜测导致问题的原因。 应用程序在cookie中存储令牌以对用户进行身份validation。 为了测试这个,我编写了一个名为signin_user(user)的辅助方法,并将其放在spec/support/utilities.rb文件中: # spec/support/utilities.rb def sign_in(user) […]

如何在rails app上点击ruby中的水豚按钮

我正在尝试选择带水豚的单选按钮,但找不到单选按钮。 这是我的rspec测试,视图和错误。 请注意我使用工厂用于用户,技能等。 Rspec测试 scenario “user chooses a couple skills and moves on to bio” do user = create(:user) skill = create(:skill) skill_two = create(:skill) skill_three = create(:skill) sign_in(user) visit onboard_skills_path choose(skill.name) end 视图 “, class: “submit_skills” %> 我得到的错误是: Unable to find radio button “Development 1”

使用Rspec在模型中测试关系和方法

我正在研究rails项目和我在rails上的新function,我想使用rspec在模型中测试我的模型关系和方法。 我怎么能这样做我的模型关系是这样的 class Idea < ActiveRecord::Base belongs_to :person belongs_to :company has_many :votes validates_presence_of :title, :description def published? if self.status == "published" return true else return false end end def image_present if self.image_url return self.image_url else return "/images/image_not_found.jpg" end end def voted self.votes.present? end end 我的idea_spec.rb文件是 require ‘spec_helper’ describe Idea do it “should have title” do […]

Capybara + Poltergeist与Database-cleaner没有找到FactoryGirl记录

我正在编写集成测试并使用FactoryGirl创建记录。 当测试具有js: true (使用Poltergeist)时,我的控制器正在抛出RecordNotFound ,即使它们是在非js测试中找到的(没有Poltergeist)。 我确实将use_transactional_fixtures设置为false ,将DatabaseCleaner.strategy设置为:truncation ,它似乎涵盖了此问题上每个现有的SO问题。 我尝试用Selenium(用Firefox)代替Poltergeist,但我得到了相同的结果。 ETA我用RSpec-Rails 3.3.3,Capybara 2.4.4,Poltergeist 1.6.0,PhantomJS 1.9.8,Database Cleaner 1.4.1开始了一个新的Rails 4.2.3项目,并在测试一个新的未经编辑的脚手架时获得相同的结果。 为什么Poltergeist找不到我的记录? 任何建议都会有所帮助,因为我已经在这里工作了几个小时。 前两个测试通过,而最后一个测试通过RecordNotFound在第二行失败: require ‘spec_helper’ before :each do @vehicle = FactoryGirl.create :vehicle end it “should work on vehicle path” do visit vehicle_path(@vehicle) expect(page).to have_content @vehicle.name end describe “with js”, js: true do it “should work on root path” […]

sRails 4.2 / Rspec / rspec-retry – 关联属于/ has_many失败

我正面临着一个关于rspec问题的怪物,每次我试图解决它创造另一个错误。 我有一个模特交易,其中有很多步骤 模型/ step.rb belongs_to :deal, :foreign_key => ‘deal_id’ 模型/ deal.rb has_many :steps, dependent: :destroy do # added to enable maximum nb of steps a deal can have # to take into account in active admin # in order to calculate the correct new nb of step to compare to the authorized limit # source: […]

使用find_by_id在RSpec中获取不存在的记录时引发RecordNotFound

我在products_controller_spec.rb中编写了这个规范,用于在不存在的记录上调用destroy时测试重定向: it “deleting a non-existent product should redirect to the user’s profile page with a flash error” do delete :destroy, {:id => 9999} response.should redirect_to “/profile” flash[:error].should == I18n.t(:slideshow_was_not_deleted) end 这是products_controller.rb中的控制器操作: def destroy product = Product.find_by_id(params[:id]) redirect_to “profile” if !product if product.destroy flash[:notice] = t(:slideshow_was_deleted) if current_user.admin? and product.user != current_user redirect_to :products, :notice => […]