Tag: rspec

使用具有Rspec视图的Factory Girl

所以我想使用Rspec和Factory Girl测试一些视图,但我不知道如何正确地将工厂分配给视图。 我在网上看到的所有信息都使用了存根,虽然存根很好,但我想保持我的rspec内容有些一致。 我想使用工厂作为版本模型,并在渲染页面时将其与页面关联,但我考虑过的所有内容似乎都被打破了。 这是一个荒谬的例子: require ‘spec_helper’ describe “catalog/editions/rationale.html.haml” do before do @edition = Factory.create(:edition) assign(:editions, @edition) render :action => ‘rationale’, :id => @edition end context “GET rationale” do it “should not have the preamble to the United States constitution” do rendered.should_not contain(/We the People of the United States/) end end end 在这里我尝试改变渲染:action =>’rationale’,:id => […]

Rspec没有路线匹配

我在rspec中遇到以下错误: 1) LandingController landing#index returns http success Failure/Error: get :index ActionController::RoutingError: No route matches {:controller=>”landing”} # ./spec/controllers/landing_controller_spec.rb:7:in `block (3 levels) in ‘ 这是考验 require ‘spec_helper’ describe LandingController do describe “landing#index” do it “returns http success” do get :index response.should be_success end end end 我以root :to => ‘landing#index’ 。 所有其他测试都在通过,只有这一个失败,有人可以帮助我理解为什么吗? 为了完整性,这是rake routes的输出 root / landing#index auth_google_oauth2_callback […]

如何“以编程方式”“迭代地”将每个类对象属性设置为一个值?

我正在使用Ruby on Rails 3.0.9和RSpec 2.我试图以下面的方式重构一些spec文件(为了测试更少的代码类似的类对象属性值): [ :attribute_a, :attribute_b, :attribute_c ].each do |attr| before do # HERE I would like to set the “current” ‘attr’ related to the # class object instance ‘attribute_’ (read below for # more information) each time the iterator is called (note: all # following attributes are NOT attr_accesible – for that […]

Rspec未定义的局部变量或方法root_path

我开始使用Rspec,但是当我运行bundle exec rspec我收到一个错误 /spec/requests/pages_spec.rb:20:in `block (2 levels) in ‘: undefined local variable or method `root_path’ for # (NameError) 我没有运行Spork或Guard,所以下面的问题不适用 未定义的局部变量或方法`root_path’(Rspec Spork Guard) 我在spec_helper.rb文件中添加了config.include Rails.application.routes.url_helpers ,但这没有帮助。 undefined局部变量或方法`root_path’Hartl的教程第5.3.2节 这是pages_spec.rb require ‘spec_helper’ describe “Pages” do describe “navigation” do def self.it_should_be_on(path_name, value=nil) before { visit path_name } it “should be on #{path_name}” do page.should have_link(‘Home’) page.should have_link(‘Inventory’) page.should have_link(‘FAQ’) […]

使用WebMockvalidationHTTPS调用

我想使用webmock来存根https 。 我们假设网关URL为https://some_gateway.com 。 做完之后: stub_request(:post, ‘https://some_gateway.com’).with(body: {}) 在规格。 我使用Net :: HTTP生成请求: Net::HTTP.post_form(URI.parse(‘https://some_gateway.com’), {}) 我收到问题,因为Webmock期望https://some_gateway.com但是收到版本添加端口433所以: http://www.secure.fake-payment.com:443/gateway_prod : http://www.secure.fake-payment.com:443/gateway_prod :443 / gateway_prod所以看不到注册存根。 我该如何处理这种行为?

Capybara :: Webkit :: Driver的未定义方法accept_modal

在Rails应用程序中,我使用RSpec(使用Capybara Webkit)来测试Delete链接是否正常工作。 在我的Rails模板中,我有: 这是我的规格: require ‘rails_helper’ describe “Deleting a movie”, js: true do it “destroys the movie and shows the movie listing without the deleted movie” do movie = Movie.create(movie_attributes) visit movie_path(movie) page.accept_confirm do click_link ‘Delete’ end expect(current_path).to eq(movies_path) expect(page).not_to have_text(movie.title) end end 我收到错误: NoMethodError: undefined method `accept_modal’ for # 它使用正确的驱动程序(Webkit),但它似乎没有找到accept_modal (必须由page.accept_confirm )。 我正在使用: capybara […]

为什么’rspec spec’比’bundle exec spec’运行得更快,使用的资源更少

下面是3个随机运行使用time rspec spec vs time bundle exec spec在一个相当简单的rails 3 app上。 不使用bundler总是更快,它使用的资源更少,6%vs 17%cpu。 我确定它与处理依赖项的bundler有关,但我想更好地理解这个问题。 我尝试练习TDD,所以当然我全天多次运行我的测试。 如果使用bundle exec会在速度和资源方面“花费”我,那么我很想找到一种方法来避免使用bundle exec 。 我使用rails 3.0.3,ruby 1.9.2,rspec 2.3,bundler 1.0.10 rspec spec 0.47s user 0.13s system 6% cpu 8.758 total rspec spec 0.47s user 0.12s system 6% cpu 8.521 total rspec spec 0.46s user 0.12s system 6% cpu 8.528 total bundle exec […]

RSpec – 测试之外的未初始化变量

对不起,我不知道如何更好地说出标题,但这里是我测试的一般概念: describe Model do let(:model) { FactoryGirl.create(:model) } subject { model } it { should be_valid } model.array_attribute.each do |attribute| context “description” do specify { attribute.should == 1 } end end end 问题是在行model.array_attribute.each do |attribute| ,我得到一个未定义的局部变量或方法model的错误。 我知道let(:model)正在运行,因为validation(以及其他function)工作正常。 我怀疑这个问题是因为它在任何实际测试之外被调用(即specify , it等)。 有关如何使其工作的任何想法?

我应该使用Selenium还是Jasmine来测试RSpec的视图文件?

我正在使用Ruby on Rails 3.2.2,cucumber-rails-1.3.0,rspec-rails-2.8.1和capybara-1.1.2以及Selenium驱动程序。 在收到我之前的问题的答案后,我有一个疑问: 我应该使用Selenium ruby​​-gem还是Jasmine ruby​​-gem来测试RSpec的视图文件? 如果是这样,因为我已经使用Selenium来测试JavaScript用于Cucumber的视图文件, 为什么 (当使用RSpec测试时)我应该使用Jasmine而不是Selenium? 也就是说,为什么要使用两个具有相同目的并制造相同东西的ruby? 一般而言,实际上, 您如何建议使用RSpec测试视图文件? …但是,它是使用RSpec测试视图文件的“正确方法”还是应该使用Cucumber测试?

在function规范中,如何测试成功调用Devise邮件程序?

我有一个用户注册function测试。 如何测试Devise确认说明是否正确发送? 我不需要测试电子邮件的内容,只需要调用邮件程序。 我在后台发邮件。 #user.rb def send_devise_notification(notification, *args) devise_mailer.send(notification, self, *args).deliver_later end 我尝试了一些适用于其他邮件程序的方法,包括 it “sends the confirmation email” do expect(Devise.mailer.deliveries.count).to eq 1 end 和 it “sends the confirmation email” do message_delivery = instance_double(ActionMailer::MessageDelivery) expect(Devise::Mailer).to receive(:confirmation_instructions).and_return(message_delivery) expect(message_delivery).to receive(:deliver_later) end 没有一个像Devise消息那样正常工作。 我究竟做错了什么? 编辑 function规范如下所示: feature “User signs up” do before :each do visit ‘/’ click_link ‘Sign up’ […]