Chuan Chuan Law

DevOps | Software Automation | Continuous Integration

Year: 2011 (page 1 of 3)

SpecFlow + C# – Read a row from database to check the value

 

 [Then(@”we verify that the (.*) row contains (.*) of (.*)”)]
        public void ThenWeVerifyThatTheFirstRowContainsLInstIDOf410(String row, String field, String value)
        {
            SqlConnection ThisConnection = new SqlConnection(@”Server=1.1.0.26;Database=pp5_410_LK;Trusted_Connection=True”);
            ThisConnection.Open();
            SqlCommand thisCommand = ThisConnection.CreateCommand();
            if (String.Compare(row,”first”)==0)
            {
                thisCommand.CommandText = “select * from CostCentre where sCostCentreNo in (select MAX(sCostCentreNo) from CostCentre where lInstID=410) and lInstID=410”;
            }
            if (String.Compare(row, “last”) == 0)
            {
                thisCommand.CommandText = “select * from CostCentre where sCostCentreNo in (select MIN(sCostCentreNo) from CostCentre where lInstID=410) and lInstID=410”;
            }
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            try
            {
                while (thisReader.Read())
                {
                    Console.WriteLine(thisReader[field].ToString());
                    Assert.AreEqual(value, thisReader[field].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            thisReader.Close();
            ThisConnection.Close();
        }

SpecFlow + C# – Execute a script and how to make step definitions reusable

//This is how we can make the step definitions reusable

[When(@”I run the (.*)”)]
            public void WhenIRunTheGenericImporter(String importer)
            {
               if (String.Compare(importer,”GenericImporter”)==0)
               {
                   StartInfo.FileName = @”C:\TrunkbinDebugWinExecutablesGenericImporter.exe”;
               }
               if (String.Compare(importer,”PageUpImporter”)==0)
               {
                   StartInfo.FileName = @”C:\TrunkbinDebugWinExecutablesPageUpImporter.exe”;
//Passing in argument to the command
                   StartInfo.Arguments = @”lk 410″;
               }

               ProcessStartInfo StartInfo = new ProcessStartInfo();

               Process Process = new Process();
               StartInfo.RedirectStandardOutput = true;
               StartInfo.RedirectStandardError = true;
               StartInfo.UseShellExecute = false;
               StartInfo.CreateNoWindow = true;
               Process.StartInfo = StartInfo;
               Process.Start();
            }

SpecFlow + C# – Connect to a SQL database

 

 [Given(@”we clean the database”)]
        public void GivenWeCleanTheDatabase()
        {
           SqlConnection ThisConnection = new SqlConnection(@”Server=1.10.26;Database=410_LK;Trusted_Connection=True”);
            ThisConnection.Open();
            SqlCommand thisCommand = ThisConnection.CreateCommand();
            thisCommand.CommandText = “delete from costcentre where linstid = 410”;
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            thisReader.Close();
            ThisConnection.Close();
        }

RSpec 2 – Stubs

Below is an example on how to use Stubs in RSpec 2:

context “percentage of review” do
     it “should be able to return the percentage of review” do
       section=Section.create(:section_config=>@section_config, :review => @review)
       item=Item.create(:section=>section)
       item.stub(:component_of_score_met).and_return(3.5)
       item.stub(:weighting_of_review).and_return(5.5)
       item.percentage_of_review.should==19.25
     end


  end

RSpec 2 – Mailer

Below is an example of a review_mailer_spec.rb:

describe ReviewMailer do
  it ‘creates and sends an email’ do
     @email=ReviewMailer.create_email(“test@com.au”,”Hello”,”Testing 123″)
     @email.to.should==[“test@com.au”]
     @email.subject.should==”Hello”
     @email.body.encoded.should match “Testing 123”
  end
end

RSpec 2 – Controllers

Below is an example of a behaviours_controller_spec.rb file:

describe ‘#update’ do
  
    it “should allow the rating of an item by the review person” do
      item=mock_model(Item)
      review=mock_model(Review)
      get :update, rating_id: review.id, id: item.id, rating: 4
      response.status.should==302


    end
  
    it “should allow the rating of an item by the review manager” do
       item=mock_model(Item)
       review=mock_model(Review)
       put :update, rating_id: review.id, id: item.id, rating: 4
       response.status.should==302
    end
  
  end

RSpec 2 – Model

Below is an example of a RSpec 2 – Model test in a item_spec.rb file:

context “maximum score” do
     it “should return maximum score of 0 when there is no item ratings” do
       section=Section.create(:section_config=>@section_config, :review => @review)
       item=Item.create(:section=>section)
       item.maximum_score.should==0
     end


     it “should return the maximum score when there is an item rating” do
       ItemRating.create(title: ‘High’, value: 4, :section_config => @section_config)
       ItemRating.create(title: ‘High’, value: 8, :section_config => @section_config)
       section=Section.create(:section_config=>@section_config, :review => @review)
       item=Item.create(:section=>section)
       item.maximum_score.should==8
     end
  end

RSpec 2 – Requests

Below is an example of a RSpec 2 – Request test looks like in a  behaviours_spec.rb file:

describe “Behaviours” do
  describe “GET /behaviours” do
    it “works! (now write some real specs)” do
      get behaviours_path
      response.status.should be(302)
    end
  end
end

RSpec 2 – Views

Below is an example of a RSpec 2 View test looks like in a html.erb_spec.rb file:

it “renders the edit framework form” do
    render


    assert_select “form”, :action => frameworks_path(@framework), :method => “post” do
        assert_select “input#framework_title”, :name => “framework[title]”
    end
  end
end

RSpec 2 – Routing

Below is an example of how a routing RSpec test is written for a Rails 3 application

In spec/routing/behaviours_routing_spec.rb

describe Admin::BehavioursController do
  describe “routing” do


    it “routes to #index” do
      get(“/behaviours”).should route_to(:controller => “admin/behaviours”,:action => “index”)
     end
end





Hint: “rake routes” shows all the routes in the application

« Older posts

© 2019 Chuan Chuan Law

Theme by Anders NorenUp ↑